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,用单例的方式完成这个目的。