`
qieyi28
  • 浏览: 152557 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring 面向切面编程(AOP)

 
阅读更多

一、什么是 AOP

面向切面编程AOP提供另外一种角度来思考程序结构,通过这种方式弥补了面向对象编程(OOP)的不足。 除了类(classes)以外,AOP提供了切面。切面对关注点进行模块化,例如横切多个类型和对象的事务管理。 (这些关注点术语通常称作 横切(crosscutting) 关注点。)

Spring的一个关键的组件就是 AOP框架。 尽管如此,Spring IoC容器并不依赖于AOP,这意味着你可以自由选择是否使用AOP,AOP提供强大的中间件解决方案,这使得Spring IoC容器更加完善。

 

二、AOP相关重要概念(必须理解)

  • 切面(Aspect): 一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。 在Spring AOP中,切面可以使用通用类(基于模式的风格) 或者在普通类中以 @Aspect 注解(@AspectJ风格)来实现。

  • 连接点(Joinpoint): 在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。 在Spring AOP中,一个连接点总是 代表一个方法的执行。 通过声明一个 org.aspectj.lang.JoinPoint 类型的参数可以使通知(Advice)的主体部分获得连接点信息。

  • 通知(Advice): 在切面的某个特定的连接点(Joinpoint)上执行的动作。通知有各种类型,其中包括“around 【环绕】”、“before【前置】”和“after【后置】”等通知。 通知的类型将在后面部分进行讨论。许多AOP框架,包括Spring,都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链。

  • 切入点(Pointcut): 匹配连接点(Joinpoint)的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例如,当执行某个特定名称的方法时)。 切入点表达式如何和连接点匹配是AOP的核心:Spring缺省使用AspectJ切入点语法。

  • 引入(Introduction): (也被称为内部类型声明(inter-type declaration))。声明额外的方法或者某个类型的字段。 Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。 例如,你可以使用一个引入来使bean实现 IsModified 接口,以便简化缓存机制。

  • 目标对象(Target Object): 被一个或者多个切面(aspect)所通知(advise)的对象。也有人把它叫做 被通知(advised) 对象。 既然Spring AOP是通过运行时代理实现的,这个对象永远是一个 被代理(proxied) 对象。

  • AOP代理(AOP Proxy): AOP框架创建的对象,用来实现切面契约(aspect contract)(包括通知方法执行等功能)。 在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。 注意:Spring 2.0最新引入的基于模式(schema-based)风格和@AspectJ注解风格的切面声明,对于使用这些风格的用户来说,代理的创建是透明的。

  • 织入(Weaving): 把切面(aspect)连接到其它的应用程序类型或者对象上,并创建一个被通知(advised)的对象。 这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。 Spring和其他纯Java AOP框架一样,在运行时完成织入。

通知的类型:

  • 前置通知(Before advice): 在某连接点(join point)之前执行的通知,但这个通知不能阻止连接点前的执行(除非它抛出一个异常)。

  • 返回后通知(After returning advice): 在某连接点(join point)正常完成后执行的通知:例如,一个方法没有抛出任何异常,正常返回。

  • 抛出异常后通知(After throwing advice): 在方法抛出异常退出时执行的通知。

  • 后通知(After (finally) advice): 当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。

  • 环绕通知(Around Advice): 包围一个连接点(join point)的通知,如方法调用。这是最强大的一种通知类型。 环绕通知可以在方法调用前后完成自定义的行为。它也会选择是否继续执行连接点或直接返回它们自己的返回值或抛出异常来结束执行。

三、spring Aop的实现

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

     1.基于 Annotation 的“零配置”方式:使用@Aspect、@Pointcut等 Annotation 来标注切入点和增强处理。

  •        2.基于 XML 配置文件的管理方式:使用 Spring 配置文件来定义切入点和增强点。

 

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

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

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns:aop="http://www.springframework.org/schema/aop"        
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">  
        <!-- 启动对@AspectJ注解的支持 -->  
        <aop:aspectj-autoproxy/>  
</beans>  

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

<!-- 启用@AspectJ 支持 -->  
<bean class="org.springframeword.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />  

 

(2)、定义切面 Bean。

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

// 使用@Aspect 定义一个切面类  
@Aspect  
public class LogAspect {  
        // 定义该类的其他内容  
        ...  
} 

 

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

// 定义一个切面  
@Aspect  
public class BeforeAdviceTest {  
    // 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点  
    @Before("execution(* com.wicresoft.app.service.impl.*.*(..))")  
    public void authorith(){  
        System.out.println("模拟进行权限检查。");  
    }  
}  

 

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

关于这个表达式的规则如下图。

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

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

 

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

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

 

 

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

 

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

 

  • AfterReturning 增强处理处理只有在目标方法成功完成后才会被织入。
  • After 增强处理不管目标方法如何结束(保存成功完成和遇到异常中止两种情况),它都会被织入。
  • // 定义一个切面  
    @Aspect  
    public class AfterAdviceTest {  
        // 匹配 com.wicresoft.app.service.impl 包下所有类的所有方法作为切入点  
        @After("execution(* com.wicresoft.app.service.impl.*.*(..))")  
        public void release() {  
            System.out.println("模拟方法结束后的释放资源...");  
        }  
    } 
     

    (7)、Around 增强处理

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

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

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

     

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

     

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

     

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

     

    (9)、定义切入点。

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

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

     

    • 一个切入点表达式。
    • 一个包含名字和任意参数的方法签名。
    • // 使用@Pointcut Annotation 时指定切入点表达式  
      @pointcut("execution * transfer(..)")  
      // 使用一个返回值为void,方法体为空的方法来命名切入点  
      private void anyOldTransfer(){}  
        
      // 使用上面定义的切入点  
      @AfterReturning(pointcut="anyOldTransfer()", returning="reVal")  
      public void writeLog(String msg, Object reVal){  
          ...  
      }  
       
    • 2、基于 XML 配置文件的管理方式。

    • 不配置切入点
    • <?xml version="1.0" encoding="UTF-8"?>  
      <beans xmlns="http://www.springframework.org/schema/beans"  
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
             xmlns:aop="http://www.springframework.org/schema/aop"        
             xsi:schemaLocation="http://www.springframework.org/schema/beans  
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                 http://www.springframework.org/schema/aop  
             http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">  
              <aop:config>  
                  <!-- 将 fourAdviceBean 转换成切面 Bean, 切面 Bean 的新名称为:fourAdviceAspect,指定该切面的优先级为2 -->  
                  <aop:aspect id="fourAdviceAspect" ref="fourAdviceBean" order="2">  
                      <!-- 定义个After增强处理,直接指定切入点表达式,以切面 Bean 中的 Release() 方法作为增强处理方法 -->  
                      <aop:after pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />  
                        
                      <!-- 定义个Before增强处理,直接指定切入点表达式,以切面 Bean 中的 authority() 方法作为增强处理方法 -->  
                      <aop:before pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="authority" />  
                        
                      <!-- 定义个AfterReturning增强处理,直接指定切入点表达式,以切面 Bean 中的 log() 方法作为增强处理方法 -->  
                      <aop:after-returning pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="log" />  
                        
                      <!-- 定义个Around增强处理,直接指定切入点表达式,以切面 Bean 中的 processTx() 方法作为增强处理方法 -->  
                      <aop:around pointcut="execution(* com.wicresoft.app.service.impl.*.*(..))" method="processTx" />  
                        
                  </aop:aspect>  
              </aop:config>  
                
              <!-- 省略各个Bean 的配置 -->  
              <!-- ... -->  
                
      </beans> 
       
    • 配置切入点
    • <?xml version="1.0" encoding="UTF-8"?>  
      <beans xmlns="http://www.springframework.org/schema/beans"  
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
             xmlns:aop="http://www.springframework.org/schema/aop"        
             xsi:schemaLocation="http://www.springframework.org/schema/beans  
                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
                 http://www.springframework.org/schema/aop  
             http://www.springframework.org/schema/beans/spring-aop-3.0.xsd">  
              <aop:config>  
                  <!-- 定义一个切入点,myPointcut,直接知道它对应的切入点表达式 -->  
                  <aop:pointcut id="myPointcut" expression="execution(* com.wicresoft.app.service.impl.*.*(..))" method="release" />  
                  <aop:aspect id="afterThrowingAdviceAspect" ref="afterThrowingAdviceBean" order="1">  
                      <!-- 使用上面定于切入点定义增强处理 -->  
                      <!-- 定义一个AfterThrowing 增强处理,指定切入点以切面 Bean 中的 doRecovertyActions() 方法作为增强处理方法 -->  
                      <aop:after-throwing pointcut-ref="myPointcut" method="doRecovertyActions" throwing="ex" />  
                  </aop:aspect>  
              </aop:config>  
                
              <!-- 省略各个Bean 的配置 -->  
              <!-- ... -->  
                
      </beans>  
       
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics