Springboot之AOP的执行顺序

AOP执行顺序验证

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

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>

切面上配置@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();
    }

}

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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值