how to 指定相同ponitcut advisor 的执行顺序

3种解决方案:

1、对应的@Aspect 类增加@Order;

2、对应的@Aspect 类implements Ordered;

3、xml 声明的Aspect 指定order属性的值。

约定:order 值越小,执行优先级越高。范围从Integer.MIN_VALUE 到Integer.MAX_VALUE。

以下是3种方案的实现:

// 1、+Order 注解
@Order(value = 1000)
@Aspect
@Component
public class StopWatchAdvice
// 2、implements Ordered接口
@Aspect
@Component
public class StopWatchAdvice implements Ordered
// 3、xml 声明类型

   
   

   
   
    
    
    
        
     
     
        
     
     
    
    
    

   
   

...

参考:Spring AOP: 织入的顺序不定义Order属性,通过切面类的定义顺序来决定通知执行的先后顺序

...

接着Insight spring advisor 初始化, Insight advisor's order排序:

完成find all eligible advisors for this class,会执行sortAdvisors(...),处理的规则如下:

Given two pieces of advice, a and b:
if a and b are defined in different aspects, then the advice in the aspect with the lowest order value has the highest precedence
if a and b are defined in the same aspect, then if one of a or b is a form of after advice, then the advice declared last in the aspect has the highest precedence. 
If neither a nor b is a form of after advice, then the advice declared first in the aspect has the highest precedence.

正常情况交给OrderComparator 完成。结果是从小到大的顺序。

另外:

aspectJ 类型的advisor 如何创建对应的InstantiationModelAwarePointcutAdvisorImpl实体,以及getAdvice的细节没有涉及。

...

设计参考:

装饰器应用以及双重同步锁单例:LazySingletonAspectInstanceFactoryDecorator。

为了确保BeanFactoryAspectInstanceFactory只会实例化一次,通过装饰器重写getAspectInstance,用单例的方式完成这个目的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值