Spring3.0 AOP 详解

一、什么是 AOP。

AOP(Aspect Orient Programming),也就是面向切面编程。可以这样理解,面向对象编程(OOP)是从静态角度考虑程序结构,面向切面编程(AOP)是从动态角度考虑程序运行过程


二、AOP 的作用。

常常通过 AOP 来处理一些具有横切性质的系统性服务,如事物管理、安全检查、缓存、对象池管理等,AOP 已经成为一种非常常用的解决方案。


三、AOP 的实现原理。

<img src="AOP代理.jpg" />

如图:AOP 实际上是由目标类的代理类实现的AOP 代理其实是由 AOP 框架动态生成的一个对象,该对象可作为目标对象使用。AOP 代理包含了目标对象的全部方法,但 AOP 代理中的方法与目标对象的方法存在差异,AOP 方法在特定切入点添加了增强处理,并回调了目标对象的方法


四、Spring 中对 AOP 的支持

Spring 中 AOP 代理由 Spring 的 IoC 容器负责生成、管理,其依赖关系也由 IoC 容器负责管理。因此,AOP 代理可以直接使用容器中的其他 Bean 实例作为目标,这种关系可由 IoC 容器的依赖注入提供。Spring 默认使用 Java 动态代理来创建 AOP 代理, 这样就可以为任何接口实例创建代理了。当需要代理的类不是代理接口的时候, Spring 自动会切换为使用 CGLIB 代理,也可强制使用 CGLIB。 

AOP 编程其实是很简单的事情。纵观 AOP 编程, 其中需要程序员参与的只有三个部分:

  • 定义普通业务组件。
  • 定义切入点,一个切入点可能横切多个业务组件。
  • 定义增强处理,增强处理就是在 AOP 框架为普通业务组件织入的处理动作。

所以进行 AOP 编程的关键就是定义切入点和定义增强处理。一旦定义了合适的切入点和增强处理,AOP 框架将会自动生成 AOP 代理,即:代理对象的方法 = 增强处理 + 被代理对象的方法


五、Spring 中 AOP 的实现。

Spring 有如下两种选择来定义切入点和增强处理。

  • 基于 Annotation 的“零配置”方式:使用@Aspect、@Pointcut等 Annotation 来标注切入点和增强处理。
  • 基于 XML 配置文件的管理方式:使用 Spring 配置文件来定义切入点和增强点。

1、基于 Annotation 的“零配置”方式。

(1)、首先启用 Spring 对 @AspectJ 切面配置的支持。

[java]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"        
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.            http://www.springframework.org/schema/aop  
  8.        http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">  
  9.         <!-- 启动对@AspectJ注解的支持 -->  
  10.         <aop:aspectj-autoproxy/>  
  11. </beans>  

如果不打算使用 Spring 的 XML Schema 配置方式,则应该在 Spring 配置文件中增加如下片段来启用@AspectJ 支持。

[java]  view plain copy
  1. <!-- 启用@AspectJ 支持 -->  
  2. <bean class="org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />  


(2)、定义切面 Bean。

当启动了@AspectJ 支持后,只要在 Spring 容器中配置一个带@Aspect 注释的 Bean, Spring 将会自动识别该 Bean 并作为切面处理。

[java]  view plain copy
  1. // 使用@Aspect 定义一个切面类  
  2. @Aspect  
  3. public class LogAspect {  
  4.         // 定义该类的其他内容  
  5.         ...  
  6. }  

(3)、定义 Before 增强处理。

[java]  view plain copy
  1. // 定义一个切面  
  2. @Aspect  
  3. public class BeforeAdviceTest {  
  4.     // 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点  
  5.     @Before("execution(* com.wicresoft.app.service.impl.*.*(..))")  
  6.     public void authorith(){  
  7.         System.out.println("模拟进行权限检查。");  
  8.     }  
  9. }  

上面使用@Before Annotation 时,直接指定了切入点表达式,指定匹配 com.wicresoft.app.service.impl包下所有类的所有方法执行作为切入点。
关于这个表达式的规则如下图。



(4)、定义 AfterReturning 增强处理。

[java]  view plain copy
  1. // 定义一个切面  
  2. @Aspect  
  3. public class AfterReturningAdviceTest {  
  4.     // 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点  
  5.     @AfterReturning(returning="rvt", pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))")  
  6.     public void log(Object rvt) {  
  7.         System.out.println("模拟目标方法返回值:" + rvt);  
  8.         System.out.println("模拟记录日志功能...");  
  9.     }  
  10. }  

(5)、定义 AfterThrowing 增强处理。

[java]  view plain copy
  1. // 定义一个切面  
  2. @Aspect  
  3. public class AfterThrowingAdviceTest {  
  4.     // 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点  
  5.     @AfterThrowing(throwing="ex", pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))")  
  6.     public void doRecoverActions(Throwable ex) {  
  7.         System.out.println("目标方法中抛出的异常:" + ex);  
  8.         System.out.println("模拟抛出异常后的增强处理...");  
  9.     }  
  10. }  

(6)、定义 After 增强处理。

After 增强处理与AfterReturning 增强处理有点相似,但也有区别:

  • AfterReturning 增强处理处理只有在目标方法成功完成后才会被织入。
  • After 增强处理不管目标方法如何结束(保存成功完成和遇到异常中止两种情况),它都会被织入。

[java]  view plain copy
  1. // 定义一个切面  
  2. @Aspect  
  3. public class AfterAdviceTest {  
  4.     // 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点  
  5.     @After("execution(* com.wicresoft.app.service.impl.*.*(..))")  
  6.     public void release() {  
  7.         System.out.println("模拟方法结束后的释放资源...");  
  8.     }  
  9. }  

(7)、Around 增强处理

Around 增强处理近似等于 Before 增强处理和  AfterReturning 增强处理的总和。它可改变执行目标方法的参数值,也可改变目标方法之后的返回值。

[java]  view plain copy
  1. // 定义一个切面  
  2. @Aspect  
  3. public class AroundAdviceTest {  
  4.     // 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点  
  5.     @Around("execution(* com.wicresoft.app.service.impl.*.*(..))")  
  6.     public Object processTx(ProceedingJoinPoint jp) throws java.lang.Throwable {  
  7.         System.out.println("执行目标方法之前,模拟开始事物...");  
  8.         // 执行目标方法,并保存目标方法执行后的返回值  
  9.         Object rvt = jp.proceed(new String[]{"被改变的参数"});  
  10.         System.out.println("执行目标方法之前,模拟结束事物...");  
  11.         return rvt + "新增的内容";  
  12.     }  
  13. }  

(8)、访问目标方法的参数。

访问目标方法最简单的做法是定义增强处理方法时将第一个参数定义为 JoinPoint 类型,当该增强处理方法被调用时,该 JoinPoint 参数就代表了织入增强处理的连接点。JoinPoint 里包含了如下几个常用方法。

  • Object[] getArgs(): 返回执行目标方法时的参数。
  • Signature getSignature(): 返回被增强的方法的相关信息。
  • Object getTarget(): 返回被织入增强处理的目标对象。
  • Object getThis(): 返回 AOP 框架为目标对象生成的代理对象。

提示当时使用 Around 处理时,我们需要将第一个参数定义为 ProceedingJoinPoint 类型,该类型是 JoinPoint 类型的子类


(9)、定义切入点。

所谓切入点,其实质就是为一个切入点表达式起一个名称,从而允许在多个增强处理中重用该名称。

Spring 切入点定义包含两个部分:

  • 一个切入点表达式。
  • 一个包含名字和任意参数的方法签名。

[java]  view plain copy
  1. // 使用@Pointcut Annotation 时指定切入点表达式  
  2. @pointcut("execution * transfer(..)")  
  3. // 使用一个返回值为void,方法体为空的方法来命名切入点  
  4. private void anyOldTransfer(){}  
  5.   
  6. // 使用上面定义的切入点  
  7. @AfterReturning(pointcut="anyOldTransfer()", returning="reVal")  
  8. public void writeLog(String msg, Object reVal){  
  9.     ...  
  10. }  

2、基于 XML 配置文件的管理方式。

  • 不配置切入点

[java]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"        
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.            http://www.springframework.org/schema/aop  
  8.        http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">  
  9.         <aop:config>  
  10.             <!-- 将 fourAdviceBean 转换成切面 Bean, 切面 Bean 的新名称为:fourAdviceAspect,指定该切面的优先级为2 -->  
  11.             <aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">  
  12.                 <!-- 定义个After增强处理,直接指定切入点表达式,以切面 Bean 中的 Release() 方法作为增强处理方法 -->  
  13.                 <aop:after pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />  
  14.                   
  15.                 <!-- 定义个Before增强处理,直接指定切入点表达式,以切面 Bean 中的 authority() 方法作为增强处理方法 -->  
  16.                 <aop:before pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="authority" />  
  17.                   
  18.                 <!-- 定义个AfterReturning增强处理,直接指定切入点表达式,以切面 Bean 中的 log() 方法作为增强处理方法 -->  
  19.                 <aop:after-returning pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="log" />  
  20.                   
  21.                 <!-- 定义个Around增强处理,直接指定切入点表达式,以切面 Bean 中的 processTx() 方法作为增强处理方法 -->  
  22.                 <aop:around pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="processTx" />  
  23.                   
  24.             </aop:aspect>  
  25.         </aop:config>  
  26.           
  27.         <!-- 省略各个Bean 的配置 -->  
  28.         <!-- ... -->  
  29.           
  30. </beans>  

  • 配置切入点

[java]  view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:aop="http://www.springframework.org/schema/aop"        
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.            http://www.springframework.org/schema/aop  
  8.        http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">  
  9.         <aop:config>  
  10.             <!-- 定义一个切入点,myPointcut,直接知道它对应的切入点表达式 -->  
  11.             <aop:pointcut id="myPointcut" expression="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />  
  12.             <aop:aspect id="afterThrowingAdviceAspect" ref="afterThrowingAdviceBean" order="1">  
  13.                 <!-- 使用上面定于切入点定义增强处理 -->  
  14.                 <!-- 定义一个AfterThrowing 增强处理,指定切入点以切面 Bean 中的 doRecovertyActions() 方法作为增强处理方法 -->  
  15.                 <aop:after-throwing pointcut-ref="myPointcut" method="doRecovertyActions" throwing="ex" />  
  16.             </aop:aspect>  
  17.         </aop:config>  
  18.           
  19.         <!-- 省略各个Bean 的配置 -->  
  20.         <!-- ... -->  
  21.           
  22. </beans>  


    《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:

  • 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
  • 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
  • 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
  • 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
  • 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config> 的 proxy-target-class 属性设为true

       通知(Advice)类型

  • 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法
  • 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,TestAspect中的doAfter方法,所以AOPTest中调用BServiceImpl.barB抛出异常时,doAfter方法仍然执行
  • 返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
  • 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,TestAspect中的doAround方法。
  • 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,TestAspect中的doThrowing方法。

    《Spring参考手册》中定义了以下几个AOP的重要概念,结合以上代码分析如下:

  • 切面(Aspect) :官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
  • 连接点(Joinpoint) :程序执行过程中的某一行为,例如,AServiceImpl.barA()的调用或者BServiceImpl.barB(String _msg, int _type)抛出异常等行为。
  • 通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如TestAspect
  • 切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定
  • 目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
  • AOP代理(AOP Proxy) 在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config> 的 proxy-target-class 属性设为true

       通知(Advice)类型

  • 前置通知(Before advice) :在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法
  • 后通知(After advice) :当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,TestAspect中的doAfter方法,所以AOPTest中调用BServiceImpl.barB抛出异常时,doAfter方法仍然执行
  • 返回后通知(After return advice) :在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
  • 环绕通知(Around advice) :包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,TestAspect中的doAround方法。
  • 抛出异常后通知(After throwing advice) : 在方法抛出异常退出时执行的通知。 ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,TestAspect中的doThrowing方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值