论坛首页 Web前端技术论坛

Spring3.0 AOP 详解

浏览 11833 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (1) :: 隐藏帖 (7)
作者 正文
   发表时间:2012-04-26  

转载自(个人在CSDN上的文章):http://blog.csdn.net/a906998248/article/details/7514969

 

一、什么是 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 plaincopy
  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 plaincopy
  1. <!-- 启用@AspectJ 支持 -->  
  2. <bean class="org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />  

 

 

(2)、定义切面 Bean。

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

 

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

 

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

 

 

[java] view plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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 plaincopy
  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. padding-top: 0px !important; padding-right: 3px !important; padding-bottom: 0px !important; padding-left: 10px !important; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: soli
   发表时间:2012-04-28  
确定这帖子放在这个版没问题?
0 请登录后投票
   发表时间:2012-04-28  
现在  spring也是前端了。。。
0 请登录后投票
   发表时间:2012-04-28  
意犹未尽的感觉~~
0 请登录后投票
   发表时间:2012-04-28  
支持下,自己工作都快2年了老实说还没有认真研究过源码...
0 请登录后投票
   发表时间:2012-04-28  
AOP的问题讲了很久了,现在还有人出,能不能创新一下。。
0 请登录后投票
   发表时间:2012-05-02  
@AspectJ   这个在类里使用是不是直接可以省略了配置文件?  以前都是通过配置文件实现的
0 请登录后投票
   发表时间:2012-05-04  
小伙,你是不是微创的啊
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics