基于Schema 配置切面:
1、切点定义的语言依然是AspectJ表达式语言
2、把不用AspectJ相关的注解定义切面,而是通过xml配置,相当于注解的xml表达的aop定义形式。
[b]定义advice增强[/b]
[b]业务方法定义[/b]
[b]
配置applicationContext.xml[/b]
详细见附件
1、切点定义的语言依然是AspectJ表达式语言
2、把不用AspectJ相关的注解定义切面,而是通过xml配置,相当于注解的xml表达的aop定义形式。
[b]定义advice增强[/b]
public class LoginMonitorAspectj {
public static final ThreadLocal<Long> time = new ThreadLocal<Long>();
public void before(JoinPoint pjp) {
time.set(System.currentTimeMillis());
System.out.println("invoke before --------------------------------");
System.out.println(pjp.toLongString());
System.out.println(Arrays.toString(pjp.getArgs()));
}
public void afterReturning(JoinPoint pjp,User user) {
System.out.println("invoke afterReturning ---------------------------------");
System.out.println(user.toString());
System.out.println("times : "+(System.currentTimeMillis() - time.get()));
}
public void _exception(JoinPoint pjp,Exception ex) {
System.out.println("invoke _exception ---------------------------------");
System.out.println("times : "+(System.currentTimeMillis() - time.get()));
}
}
[b]业务方法定义[/b]
@Service
public class LoginService implements ILoginService {
public User login(String username, String password) {
User user = new User();
System.out.println("in login method");
user.setPassword("123456");
user.setUsername("admin");
return user;
}
@MethodExecuteTimes
public void regist(String username, String password, String email, int age) {
System.out.println(Arrays.toString(new Object[]{username,password,age}));
}
}
[b]
配置applicationContext.xml[/b]
<context:component-scan base-package="org.job">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<aop:aspectj-autoproxy/>
<bean id="adviceMethods" class="org.job.user.aop.LoginMonitorAspectj"/>
<aop:config>
<aop:aspect ref="adviceMethods">
<aop:before pointcut="execution(* login(..))" method="before"/>
<aop:after-returning pointcut="execution(* login(..))" method="afterReturning" returning="user"/>
<aop:after-throwing pointcut="execution(* login(..))" method="_exception" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>
详细见附件