spring 多个切面的执行顺序及原理

本文介绍了Spring AOP中多个切面的执行顺序,可以通过实现Ordered接口、使用@Order注解或配置文件来设定。当切面有相同的order时,将按照类名字母顺序执行。在同一个切面内的不同通知方法,会按照注解顺序及字母顺序执行。文章还提供了一个示例展示了如何在事务提交后执行自定义AOP的策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近和同事聊起来了springAOP的话题,说了到多个切面的时候程序是怎么执行的,我们常用的spring事务本身也是一个切面,使用的AOP原理,本人从网上找了一些资料,然后根据这些资料进行一下总结。

资料地址:1.https://blog.youkuaiyun.com/stpice/article/details/102733782

                          2.https://www.jb51.net/article/179078.htm

另外,如果有些朋友不是很清楚aop原理,可以去找我前面写的博客,讲解的简单易懂

正文:

配置AOP执行顺序的三种方式:

通过实现org.springframework.core.Ordered接口

1

2

3

4

5

6

7

8

9

10

@Component

@Aspect

@Slf4j

public class MessageQueueAopAspect1 implements Ordered{ @Override

  public int getOrder() {

    // TODO Auto-generated method stub

    return 2;

  }

    

}

通过注解

1

2

3

4

5

6

7

8

@Component

@Aspect

@Slf4j

@Order(1)

public class MessageQueueAopAspect1{

    

  ...

}

通过配置文件配置

1

2

3

4

5

6

<aop:config expose-proxy="true">

  <aop:aspect ref="aopBean" order="0"

    <aop:pointcut id="testPointcut" expression="@annotation(xxx.xxx.xxx.annotation.xxx)"/> 

    <aop:around pointcut-ref="testPointcut"

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值