aop执行顺序,以及多个aop监控同一个接口的执行顺序

结论:

单个aop执行顺序

Around -> doBefore ->执行方法->AfterReturning -> After -> Around

多个aop执行顺序

Around1 -> doBefore1 ->Around2 -> doBefore2->执行方法->AfterReturning2 -> After2 -> Around2->AfterReturning1 -> After1 -> Around1

如果接口异常,最后的around不执行

Around1 -> doBefore1 ->Around2 -> doBefore2->执行方法->AfterReturning2 -> After2 ->AfterReturning1 -> After1

1、新建两个aop

@Aspect
@Component
@Order(100)
public class aop1 {
    @Pointcut("execution(public * com.example.aop.controller..*.*(..))")
    public void order(){}

    //声明前置通知
    @Before("order()")
    public void doBefore(JoinPoint point) {
        System.out.println("aop1==============doBefore");
        return;
    }

    //声明后置通知
    @AfterReturning(pointcut = "order()", returning = "returnValue")
    public void doAfterReturning(JoinPoint point,Object returnValue) {
        System.out.println("aop1==============AfterReturning");
    }

    //声明例外通知
    @AfterThrowing(pointcut = "order()", throwing = "e")
    public void doAfterThrowing(Exception e) {
        System.out.println("aop1==============AfterThrowing");
    }

    //声明最终通知
    @After("order()")
    public void doAfter() {
        System.out.println("aop1==============After");
    }

    //声明环绕通知
    @Around("order()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("aop1==============Around");
        Object obj = pjp.proceed();
        System.out.println("aop1==============Around");
        return obj;
    }
}
@Component
@Aspect
@Order(1001)
public class aop2 {
    @Pointcut("execution(public * com.example.aop.controller..*.*(..))")
    public void order(){}

    //声明前置通知  
    @Before("order()")
    public void doBefore(JoinPoint point) {
        System.out.println("aop2==============doBefore");
        return;
    }

    //声明后置通知  
    @AfterReturning(pointcut = "order()", returning = "returnValue")
    public void doAfterReturning(JoinPoint point,Object returnValue) {
        System.out.println("aop2==============AfterReturning");
    }

    //声明例外通知  
    @AfterThrowing(pointcut = "order()", throwing = "e")
    public void doAfterThrowing(Exception e) {
        System.out.println("aop2==============AfterThrowing");
    }

    //声明最终通知  
    @After("order()")
    public void doAfter() {
        System.out.println("aop2==============After");
    }

    //声明环绕通知  
    @Around("order()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("aop2==============Around");
        Object obj = pjp.proceed();
        System.out.println("aop2==============Around");
        return obj;
    }
}

2、测试

image.png

3、接口异常的测试

image.png

你可能感兴趣的:(aop执行顺序,以及多个aop监控同一个接口的执行顺序)