【学习】Spring 的 AOP :基于Annotation 的“零配置”方式

AOP(Aspect Orient Programming ) , 面向切面编程 。 

1、AOP的基本概念:

  AOP框架并不与特定的代码耦合,AOP框架能处理程序执行中特定的切入点(Pointcut),而不与具体某个类耦合。AOP框架具有如下特征:

  1、各步骤之间的良好隔离性。

  2、源代码无关性。

  AOP的专业术语:

  1、Aspect(切面) : 应用运行过程中的关注点,关注点可以横切多个对象,被称为横切关注点。

  2、pointcut(切入点):可插入增强处理的连接点。

  3、joinpoint(连接点):程序执行过程中明确的点,如方法的调用,或者异常的抛出。

  4、advice(增强处理):AOP框架特定的切入点执行的增强处理

如何使用表达式来定义切入点:

  1、引入:将方法或字段添加到被处理的类中。

  2、目标对象:被AOP框架进行增强处理的对象,也被称为被增强的对象。如果AOP框架是通过运行时代理来实现的,那么这个对象是一个被代理的对象。

  3、AOP代理:AOP框架创建的对象,简单的说,代理就是对目标对象的增强。

  4、织入(Weaving):将增强处理添加到目标对象中,并创建一个被增强的处理的过程就是织入。

2、Spring的 AOP支持

  在AOP编程,我们需要做如下三部分:

  1、定义普通组件。

  2、定义切入点、一个切入点可能横切多个业务组件。

  3、定义增强处理,增强处理就是在AOP框架为普通业务组件织入的处理动作。

  AOP编成的关键是 定义切入点 和 定义增强处理,Spring依然有如下两个方法来实现:

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

  2、使用XML配置文件的管理方式。

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

  为了启动Spring对@AspectJ切面配置的支持,并保证Spring容器中的目标Bean被一个或多个切面自动增强,必须在Spring配置文件下配置如下代码:

  

<?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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <!-- 启动@AspectJ支持-->
   <aop:aspectj-autoproxy />
</beans>

  但是希望完全启动Spring的“零配置”功能,则配置代码中还需添加:

  

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- 启动@AspectJ支持-->
   <aop:aspectj-autoproxy />
</beans>

   

  这里给出下面 增强处理的目标类:

  

package org.service.imp;

import org.service.Person;
import org.springframework.stereotype.*;

@Component
public class Chinese implements Person {

    public String sayHello(String name) {
        // TODO Auto-generated method stub
        return name + "你好 , Spring Aop";
    }

    public void eat(String food) {
        // TODO Auto-generated method stub
        System.out.println("我正在吃:" + food) ;
    }

}

  3.1 定义切面Bean

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

使用@Aspect标注一个Java类,该Java类将会作为切面Bean  

@Aspect
public class AspectTest
{
   //定义类的其他类容 ...... }

  3.2 定义Before增强处理

  使用@Before来标注一个方法,使该方法作为一个Before增强处理。使用@Before标注时,通常需要指定一个value值,改值指定一个切入表达式,用于指定该增强处理将被织入哪些切入点。

  如:@Before("execution(* com.cnblogs.jbelial.*.*(..))") 

  在被它标注的方法将匹配com.cnblogs.jbelial包下的所有类的、所有方法的执行作为切入点。

  如: 

package org.advice.Before;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

//定义一个切面
@Aspect
public class BeforeAdviceTest {
    //匹配该包下的所有类
    @Before( "execution(* org.service.imp.*.*(..))")
    public void authority()
    {
        System.out.println("模拟执行权限检查") ;
    }
}

  注:在定义增强处理时的Spring配置文件如何配置,可以参考Spring 的“零配置”支持“ 的学习

  

  3.3 定义AfterReturning增强处理

   类似于使用@Before ,使用@AfterReturning 来标注一个增强处理,该增强处理将会在目标方法正常完成后被织入。在使用@AfterReturning Annotation时可以指定如下两个常量属性。

  1、pointcut/value : 用于指定切入点对于的表达式。

  2、returning :制定一个返回值形参名,增强处理定义的方法可以通过该形参名来访问目标方法的返回值。

  如:@AfterReturning(returning ="rvt" , pointcut = "execution(* com.cnblogs.jbelial.*.*(..))") ; 

  指定一个 returning 属性,该属性值为 rvt , 表示 允许在 增强处理方法中使用名为rvt的形参,该形参代表目标方法的返回值。

  @AfterReturning Annotation 的 returning 属性所制定的的形参名必须对应于增强处理中的一个形参名。当目标方法执行返回后,返回值作为相应的参数值传入增强处理的方法。

  如下:

package org.advice.AfterReturning;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AfterReturningAdivceTest {
    @AfterReturning( returning = "rvt" ,
            pointcut = "execution(* org.service.imp.*.*(..))")
    public void log(Object rvt)
    {
        System.out.println("获取目标方法返回值:"+rvt) ; 
        System.out.println("模拟记录日志功能...") ; 
    }
}

  3.4 定义AfterThrowing 增强处理:

  AfterThrowing 增强处理主要用于处理程序中未处理的异常。

  @AfterThrowing Annotation 两个常用的属性:

   > pointcut/value : 如上。

   > throwing : 指定一个返回参数名,增强处理定义的方法可通过该形参名来访问目标方法中所抛出的异常对象。

  如:@AfterThrowing(throwing = "ex" ,pointcut = "execution(* com.cnblogs.jbelial.*.*(..))")

  

  3.5 After 增强处理:

  After 与 AfterReturning 的区别:

  After : 增强处理不管目标方法如何结束,他都会被织入。因此,After 增强处理必须准备处理正常返回和异常返回两种情况,这种增强处理通常用于释放资源

  AfterReturning : 增强处理只有在目标方法成功完成才会被织入。

  如下: 

package org.advice.After;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.After;
@Aspect
public class AfterAdviceTest 
{
    
    @After ("execution(* org.service.imp.*.*(..))")
    
    public void release()
    {
        System.out.println("模拟方法结束后的释放资源....");
    }    
}

  

  3.6 Around 增强处理 :

   Around 增强处理既可以在执行目标方法之前织入增强处理,可以在执行目标方法之后织入增强动作。同时,Around 增强处理甚至可以决定目标方法在什么时候执行,如何执行,甚至可以完全阻止目标方法的执行。

   Around 增强处理可以改变执行目标方法的参数值,也可以改变执行目标方法之后的返回值。

   当定义一个 Around 增强处理方法时,该方法的第一个形参必须是 ProceedingJoinPoint 类型,调用 ProceedingJoinPoint 的 proceed() 方法才会执行目标方法。

  如下: 

package org.advice.Around;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
@Aspect
public class AroundAdviceTest {
    @Around ( "execution(* org.service.imp.*.*(..))")
    public void processText(ProceedingJoinPoint PJ) throws Throwable
    {
        System.out.println("执行目标方法之前,模拟开始事务。");
        PJ.proceed() ; 
        System.out.println("执行目标方法之后,模拟结束事务。");
    }
}

  

  3.7  访问目标方法的参数:

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

  > Object[] getArgs() :  返回执行目标方法时的参数。  

  > Signature getSignature() : 返回被增强的方法的相关信息。

  > Object getTarget() : 返回被织入增强处理的目标对象。

  > Object getThis() : 返回AOP框架为目标的相关信息。

  注意:ProceedingJoinPoint 该类是JoinPoint 的子类。

  

   3.8 定义切入点:

  为一个切入表达式起一个名称,从而允许在多个增强处理中重用该名称。Spring AOP 只支持以Spring Bean 的方法执行组 作为连接点,所以可以把切入点看错所有能和切入表达式匹配的Bean 方法。

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

  > 一个切入点表达式。

  > 一个包含名字和任意参数的方法签名。

  如下: 

package org.advice.AfterReturning;

import org.aspectj.lang.annotation.*;

@Aspect
public class adviceExpression {
    @Pointcut("execution(* org.service.imp.*.*(..))")
    public void test() {  }
}

  注意:上面代码中的 test方法的返回值必须是void , 且当用private 访问控制符修饰时,则仅能在切面类中使用该切入点。

package org.advice.AfterReturning;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect; 
@Aspect
public class AfterReturningAdivceTest {
    @AfterReturning( returning = "rvt" ,
            pointcut = "adviceExpression.test()")
    public void log(Object rvt)
    {
        System.out.println("获取目标方法返回值:"+rvt) ; 
        System.out.println("模拟记录日志功能...") ; 
    }
}

  

  3.9 切入点指示符

  Spring AOP 仅仅支持部分 Aspect J 的切入指示符。

  Spring AOP 支持的切入点指示符有:

    > exection : 用于匹配执行方法的连接点,

    > within : 限定匹配特定类型的连接点,当使用Spring AOP 的时候,只能匹配方法执行的连接点。

        如下:

          within(org.service.imp.*) // imp 包中的任意连接点

          within(org.service.imp..*) // imp 包或子包中的任意连接点

    > this : 用于限定AOP 代理必须是指定类型的实例,用于匹配该对象的所有连接点。当使用Spring AOP 的时候,只能匹配方法执行的连接点。

    > target : 用于限定目标对象必须是指定类型的实例,用于匹配该对象的所有连接点。当使用Spring AOP 的时候,只能匹配方法执行的连接点。

        如下:

          // 匹配实现了Person 接口的目标对象的所有连接

          // 在Spring AOP 中只是方法执行的连接点

          target(org.service.imp.Person)

    > args : 用于对限定目标参数类型的限定,要求参数类型是指定类型的实例。当使用Spring AOP 的时候,只能匹配方法执行的连接点。

   Spring 支持使用如下路基运算符来组合切入点表达式。

    > !! : 只要连接点匹配任意一个切入点表达式。

    > && : 

    > ! : 要求连接点不匹配指定切入点表达式。

原文地址:https://www.cnblogs.com/jbelial/p/2539123.html