spring AOP操作
在spring进行AOP操作,使用aspectj实现
一、aspectj准备
aspectj不是spring的一部分,和spring一起使用进行AOP的操作
1.除了spring基本的jar包还需要导入与aspectj有关的jar包


相关的jar包可以在www.java2s.com上下载
2.xml引入aop的约束

二、aspectj实现(xml方式)
示例:使用myLog中的before方法对Log类中的add方法进行前置增强
1.xml配置文件

配置切入点需使用表达式。使用execution函数
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution(* test.Log.add(..)) 对Log类中的任意修饰符的add方法进行增强,".."表示包括带参数的。
(2)execution(* test.Log.*(..)) 对Log类中的所有方法进行增强。
(3)execution(* *.*(..)) 对所有类的所有方法进行增强
2.Log类

3.myLog类

4.测试类和结果


三、aspect实现(注解方式)
示例:使用myLog中的before方法对Log类中的add方法进行前置增强
1.xml配置文件

需开启aop注解扫描
2.Log类

3.MyLog类

切入点仍需要用execution函数表示
4.测试类和结果


浙公网安备 33010602011771号