AOP注解方式

Aop  aspect object programming  面向切面编程

         功能: 让关注点代码与业务代码分离!

关注点,

         重复代码就叫做关注点;

切面,

          关注点形成的类,就叫切面(类)!

          面向切面编程,就是指 对很多功能都有的重复的代码抽取,再在运行的时候网业务方法上动态植入“切面类代码”。

切入点,

         执行目标对象方法,动态植入切面代码。

         可以通过切入点表达式,指定拦截哪些类的哪些方法; 给指定的类在运行的时候植入切面类代码。

1 所需jar文件:

  

 先引入aop相关jar文件         (aspectj  aop优秀组件)                                      

        spring-aop-3.2.5.RELEASE.jar   【spring3.2源码】

  aopalliance.jar                              【spring2.5源码/lib/aopalliance】

  aspectjweaver.jar                       【aspectj-1.8.2lib】

  aspectjrt.jar                                   【aspectj-1.8.2lib】

注意: 用到spring2.5版本的jar文件,如果用jdk1.7可能会有问题。

                   需要升级aspectj组件,即使用aspectj-1.8.2版本中提供jar文件提供。

2) bean.xml中引入aop名称空间

以下内容部分转载自  http://blog.csdn.net/autfish/article/details/51184405

2 注解说明 

2.1 @Aspect

作用是把当前类标识为一个切面供容器读取

2.2 @Before
标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有

2.3 @AfterReturning

后置增强,相当于AfterReturningAdvice,方法正常退出时执行

2.4 @AfterThrowing

异常抛出增强,相当于ThrowsAdvice

2.5 @After

final增强,不管是抛出异常或者正常退出都会执行

2.6 @Around

环绕增强,相当于MethodInterceptor

实例:

 1 bean.xml
 2 <!-- IOC容器的配置: 开启注解扫描和自动注入 -->
 3 
 4 <context:component-scan base-package="annoaop" />
 5     
 6 <aop:aspectj-autoproxy />
 7 
 8 AOP类
 9 @Component
10 @Aspect  // 指定当前类为切面类
11 public class Aop {
12 
13     // 指定切入点表单式: 拦截哪些方法; 即为哪些类生成代理对象
14     
15     @Pointcut("execution(* cn.itcast.e_aop_anno.*.*(..))")
16     public void pointCut_(){
17     }
18     
19     // 前置通知 : 在执行目标方法之前执行
20     @Before("pointCut_()")
21     public void begin(){
22         System.out.println("开始事务/异常");
23     }
24     
25     // 后置/最终通知:在执行目标方法之后执行  【无论是否出现异常最终都会执行】
26     @After("pointCut_()")
27     public void after(){
28         System.out.println("提交事务/关闭");
29     }
30     
31     // 返回后通知: 在调用目标方法结束后执行 【出现异常不执行】
32     @AfterReturning("pointCut_()")
33     public void afterReturning() {
34         System.out.println("afterReturning()");
35     }
36     
37     // 异常通知: 当目标方法执行异常时候执行此关注点代码
38     @AfterThrowing("pointCut_()")
39     public void afterThrowing(){
40         System.out.println("afterThrowing()");
41     }
42     
43     // 环绕通知:环绕目标方式执行
44     @Around("pointCut_()")
45     public void around(ProceedingJoinPoint pjp) throws Throwable{
46         System.out.println("环绕前....");
47         pjp.proceed();  // 执行目标方法
48         System.out.println("环绕后....");
49     }
50     
51 }
52 
53 测试类
54 ApplicationContext ac = 
55         new ClassPathXmlApplicationContext("cn/itcast/e_aop_anno/bean.xml");
56 
57     // 目标对象有实现接口,spring会自动选择“JDK代理”
58     @Test
59     public void testApp() {
60         IUserDao userDao = (IUserDao) ac.getBean("userDao");
61         System.out.println(userDao.getClass());//$Proxy001  
62         userDao.save();
63     }
64 
65 注意:如果目标对象有接口切使用jdk代理那么只能用接口接收(看jdk代理原理)

3 execution切点函数

execution函数用于匹配方法执行的连接点,语法为:

execution(方法修饰符(可选)  返回类型  方法名  参数  异常模式(可选)) 

参数部分允许使用通配符:

*  匹配任意字符,但只能匹配一个元素

.. 匹配任意字符,可以匹配任意多个元素,表示类时,必须和*联合使用

+  必须跟在类名后面,如Horseman+,表示类本身和继承或扩展指定类的所有类

示例中的* chop(..)解读为:

方法修饰符  无

返回类型      *匹配任意数量字符,表示返回类型不限

方法名          chop表示匹配名称为chop的方法

参数               (..)表示匹配任意数量和类型的输入参数

异常模式       不限

4 更多切点函数

除了execution(),Spring中还支持其他多个函数,这里列出名称和简单介绍,以方便根据需要进行更详细的查询

4.1 @annotation()

表示标注了指定注解的目标类方法

例如 @annotation(org.springframework.transaction.annotation.Transactional) 表示标注了@Transactional的方法

4.2 args()

通过目标类方法的参数类型指定切点

例如 args(String) 表示有且仅有一个String型参数的方法

4.3 @args()

通过目标类参数的对象类型是否标注了指定注解指定切点

如 @args(org.springframework.stereotype.Service) 表示有且仅有一个标注了@Service的类参数的方法

4.4 within()

通过类名指定切点

如 with(examples.chap03.Horseman) 表示Horseman的所有方法

4.5 target()

通过类名指定,同时包含所有子类

如 target(examples.chap03.Horseman)  且Elephantman extends Horseman,则两个类的所有方法都匹配

4.6 @within()

匹配标注了指定注解的类及其所有子类

如 @within(org.springframework.stereotype.Service) 给Horseman加上@Service标注,则Horseman和Elephantman 的所有方法都匹配

4.7 @target()

所有标注了指定注解的类

如 @target(org.springframework.stereotype.Service) 表示所有标注了@Service的类的所有方法

4.8 this()

大部分时候和target()相同,区别是this是在运行时生成代理类后,才判断代理类与指定的对象类型是否匹配

5 逻辑运算符

表达式可由多个切点函数通过逻辑运算组成

5.1 &&

与操作,求交集,也可以写成and

例如 execution(* chop(..)) && target(Horseman)  表示Horseman及其子类的chop方法

5.2 ||

或操作,求并集,也可以写成or

例如 execution(* chop(..)) || args(String)  表示名称为chop的方法或者有一个String型参数的方法

5.3 !

非操作,求反集,也可以写成not不过not前面需加一个空格

例如 execution( not * ......)

原文地址:https://www.cnblogs.com/webyyq/p/7475244.html