Spring AOP之多切面运行顺序

多切面运行顺序

当一个方法的执行被多个切面共同切的时候,环绕通知只影响当前切面的通知顺序,例如创建两个切面logUtil,validateUtil两个切面共同监视计算器类的加法运算,add(int a,int b);测试中,看切面工具类的名称首字母,默认情况下a-z执行顺序,所以这个时候logUtil切面通知比validateUtil先执行通知;
所以顺序是:L的前置通知 -->v的前置通知–>执行add方法,然后v的后置通知–>V的后置返回–>L的后置通知–>L的后置返回。
但是当logUtil中加入了环绕通知,所以环绕通知要比logUtil的普通通知先执行,环绕通知功能很强大,在通过反射执行方法的前面我们可以更改这个方法的参数,但是普通通知不能这么做。虽然在logUtil加了环绕通知,但是这个环绕通知只是比logUtil的普通通知先执行无论是进入切面前还是出切面时,他并不影响validateUtil这个切面的普通通知的执行顺序,所以加了环绕通知执行顺序是
环绕前置–> log前置–>va前置–>目标方法–>va后置–>va返回–>环绕返回通知–>环绕后置–>log后置–>log返回。

图:
这里的这里的validate切面就是图中的VaAspect;

对啦,可以更改默认的切面顺序,要在将要更改的切面类上加入@order(int value)注解,value默认值很大,超级大,越大执行的优先级越低,所以如果把它调成1就是先执行这个切面的通知。

AOP的应用场景:

  1. aop可以进行日志记录;

  2. aop可以做权限验证

  3. AOP可以做安全检查

  4. AOP可以做事务控制

回忆基于注解的AOC配置

  • 将目标类和切面类都加入到IOC容器中。@Component

  • 告诉Spring哪个是切面类@Aspect

  • 在切面类中使用五个通知注解来配置切面中的这些方法都什么时候在那运行

  • 开启注解的aop功能。

不使用注解实现AOP配置。

1.切面类
public class LogUtil {
    public void performance(){}

    public void logStart(JoinPoint joinPoint)
    {
        //获取方法上的参数列表
        Object[] args = joinPoint.getArgs();
        //获取方法签名
        Signature signature = joinPoint.getSignature();

        String name = signature.getName();//获取方法名
        System.out.println("前置通知:"+name+" 方法开始执行了....参数是:"+ Arrays.asList(args) +"");
    }

    public void logReturn(JoinPoint point,Object result)
    {
        String name = point.getSignature().getName();
        Object[] args = point.getArgs();
        System.out.println("返回通知: "+name+"方法正常执行,返回结果是:"+result+"");
    }


    public void logException(JoinPoint point,Exception e)
    {
        String name = point.getSignature().getName();
        System.out.println("异常通知:"+name+" 方法出现了异常,异常是 "+e+"...");
    }

    public void logEnd(JoinPoint joinPoint)
    {
        String name = joinPoint.getSignature().getName();
        System.out.println("后置通知:"+name+"方法结束了");
    }

    //环绕通知
    public Object myAround(ProceedingJoinPoint proceedingJoinPoint){
        Object proceed = null;
            //获取方法名
        String name = proceedingJoinPoint.getSignature().getName();
            //获取执行方法的参数列表
        Object[] args = proceedingJoinPoint.getArgs();

        try {
            System.out.println("环绕前置通知:"+name+"方法开始执行了,参数是"+Arrays.asList(args)+"");
            //等于 method.invoke();通过反射执行指定方法
            proceed = proceedingJoinPoint.proceed();
            System.out.println("环绕返回通知:"+name+"方法返回结果是"+proceed+";");
        } catch (Throwable throwable) {
            System.out.println("环绕异常通知:异常是"+throwable+"");


            throwable.printStackTrace();
        }finally {
            System.out.println("环绕后置通知:"+name+"方法结束了");
        }
        return proceed;
    }
2.被切入的类(这里是一个计算器类)
package main.java.cn.zixue.domain;public class MyCalculator
{   
  public int add(int a,int b)   
 {
         return a+b;   
 }    
 public int sub(int a,int b) 
 {        
 	return a-b;    
 }
     public int mul(int a,int b)
 {        
 	return a*b;   
  }    
  public int dev(int a,int b)
  {        
  		return a/b;    
  }    
  public double add(double a,float b,int c) 
  {
          return a+b+c;    
   }
 }
3.配置文件
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <context:component-scan base-package="main.java.cn"></context:component-scan>

    <bean id="myCalculator" class="main.java.cn.zixue.domain.MyCalculator"></bean>
    <bean id="logUtil" class="main.java.cn.zixue.utils.LogUtil"></bean>


    <!--AOP名称空间-->


    <aop:config>
      <!--  制定切面的方法-->
        <aop:pointcut id="performance" expression="execution(public * main.java.cn.zixue.domain.MyCalculator.*(..))"></aop:pointcut>
        <!--指定切面-->
        <aop:aspect ref="logUtil">
            <aop:after method="logEnd" pointcut-ref="performance"></aop:after>
            <aop:before method="logStart" pointcut-ref="performance"></aop:before>
            <aop:after-returning method="logReturn" pointcut-ref="performance" returning="result"></aop:after-returning>
            <aop:after-throwing method="logException" pointcut-ref="performance" throwing="e"></aop:after-throwing>
            <aop:around method="myAround" pointcut-ref="performance"></aop:around>
        </aop:aspect>
    </aop:config>

</beans>
4.测试结果

@Test
public void Test02()
{
    MyCalculator myCalculator = (MyCalculator) context.getBean("myCalculator");
    myCalculator.add(1,10);
    System.out.println("========================");
}

前置通知:add 方法开始执行了…参数是:[1, 10]
环绕前置通知:add方法开始执行了,参数是[1, 10]
环绕返回通知:add方法返回结果是11;
环绕后置通知:add方法结束了
返回通知: add方法正常执行,返回结果是:11
后置通知:add方法结束了
====================**

普通前置通知->环绕通知->环绕返回->环绕后置->普通返回->普通后置

注解和配置文件在什么时候使用?该如何选择?

注解的优点:配置快速简洁。

配置文件的优点:功能丰富,注解有的他都可以实现,注解没有的他也有。

当遇到重要的切面时,用配置文件写,例如权限验证及管理。对于常用的普通的切面就用注解。

原文地址:https://www.cnblogs.com/itjiangpo/p/14181444.html