我只是在编写一个非常基本的Aspect @before
函数,因为我是 AspectJ 的新手。
我已阅读spring文档并在我的示例中进行了实验。 但Aspect看起来并没有与我的代码编织在一起。
这是我的控制器,我希望提供哪些方法。 请注意,这是一个简单的java方法,而不是任何请求方法。
public void printData()
{
System.out.println("just to test aspect programming");
}
从我的Web应用程序中的一个请求方法调用它。
@RequestMapping("/")
public String doLogin(ModelMap modelMap)
{
System.out.println("doLogin" +loginService);
String message=messageSource.getMessage("message", null, "default", Locale.UK);
System.out.println("Message is:" + message);
printData();
LoginForm loginForm=new LoginForm();
modelMap.put("loginForm",loginForm);
return "login";
}
My Aspect class is as follows:
package com.neha.javabrains;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Component
@Aspect
public class LogTime {
@Before("execution(* com.neha.javabrains.LoginController.printData(..))")
public void loggedData()
{
System.out.println("In Pointcut Expression");
}
}
我的配置类如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- <bean/> definitions here -->
<context:component-scan base-package="com.neha.javabrains" />
<context:annotation-config/>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<mvc:resources mapping="/resources/**" location="/resources/" />
<tx:annotation-driven transaction-manager="txManager"/>
<aop:aspectj-autoproxy />
<bean id="loginDAO" class="com.neha.javabrains.LoginDAO">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="mySessionFactory"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="myDataSource"/>
</bean>
<bean id="loginFormValidator" class="com.neha.javabrains.LoginFormValidator"/>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="message" />
<property name="defaultEncoding" value="UTF-8" />
</bean>
<beans profile="dev">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:books"></property>
<property name="username" value="system"></property>
<property name="password" value="xyz"></property>
</bean>
</beans>
<beans profile="prod">
<bean id="myDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="system"></property>
<property name="password" value="abc"></property>
</bean>
</beans>
</beans>
当我运行我的应用程序时,它工作正常但只是ASpect Before method
没有被执行,因为它的sysout没有被打印。
好像Aspect甚至没有附加到我的方法上。
请帮助!!!!!!!!!!!