Springboot之AOP的执行顺序

AOP执行顺序验证

  项目引入了依赖。自动开启了aop的配置。


            org.springframework.boot
            spring-boot-starter-aop
 

切面上配置@Order注解

  切面类AopTest1,order定义为2

@Component
@Aspect
@Order(2)
public class AopTest1 {
    @Pointcut("execution( * com.example.service.AopService.*(..))")
    public void pointCutMethod() {
    }
    @After("pointCutMethod()")
    public void myAfter(JoinPoint point) {
        System.out.println("我是AopTest1#myAfter");
    }
    @Before("pointCutMethod()")
    public void myBefore(JoinPoint point) {
        System.out.println("我是AopTest1#myBefore");
    }
    @Before("pointCutMethod()")
    public void myBefore2(JoinPoint point) {
        System.out.println("我是AopTest1#myBefore2");
    }
    @Around("pointCutMethod()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("我是AopTest1#around before");
        Object res = joinPoint.proceed();
        System.out.println("我是AopTest1#around after");
        return res;
    }
    @AfterThrowing(value = "pointCutMethod()", throwing = "e")
    public void afterThrowing(Exception e) {
        System.out.println("我是AopTest1#afterThrowing");
    }
    @AfterReturning(value = "pointCutMethod()")
    public void afterReturning() {
        System.out.println("我是AopTest1#afterReturning");
    }
}

  切面类AopTest2,order定义为1

@Component
@Aspect
@Order(1)
public class AopTest2 {
    @Pointcut("execution( * com.example.service.AopService.*(..))")
    public void pointCutMethod() {

    }
    @After("pointCutMethod()")
    public void myAfter(JoinPoint point) {
        System.out.println("我是AopTest2#myAfter");
    }

    @Before("pointCutMethod()")
    public void myBefore(JoinPoint point) {
        System.out.println("我是AopTest2#myBefore");
    }

    @Before("pointCutMethod()")
    public void myBefore2(JoinPoint point) {
        System.out.println("我是AopTest2#myBefore2");
    }


    @Around("pointCutMethod()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("我是AopTest2#around before");
        Object res = joinPoint.proceed();
        System.out.println("我是AopTest2#around after");
        return res;
    }

    @AfterThrowing(value = "pointCutMethod()", throwing = "e")
    public void afterThrowing(Exception e) {
        System.out.println("我是AopTest2#afterThrowing");
    }


    @AfterReturning(value = "pointCutMethod()")
    public void afterReturning() {
        System.out.println("我是AopTest2#afterReturning");
    }
}

  测试业务类AopService,里面会抛出异常1/0

@Service
public class AopService {
    public void testMethod(){
        System.out.println("我是AopService#testMethod");
        int b = 1/0;
    };
}

  启动类。获取AopService,调用testMethod方法。

@SpringBootApplication
public class SongDm2Application {

    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(SongDm2Application.class, args);
        AopService aopService = run.getBeanFactory().getBean(AopService.class);
        aopService.testMethod();
    }

}

  执行结果如下。
Springboot之AOP的执行顺序_第1张图片
  目标对象方法去掉抛出异常,执行结果如下
Springboot之AOP的执行顺序_第2张图片
Springboot之AOP的执行顺序_第3张图片
  总结。AOP的执行顺序如下:
  1.目标方法抛异常:Around before–>Before–>目标方法–>AfterThrowing–>After
  2.目标方法不抛异常:Around before–>Before–>目标方法–>AfterReturning–>After–>Around After
  3.同一个切面内的多个同一类型的拦截方法。按字符排序。
  4.多个切面拦截同一个方法,受@Order排序影响。目标方法执行之前,和执行之后的拦截顺序看上面的实验结果。

你可能感兴趣的:(springboot,spring,boot,java,后端)