Spring源码-Aspect-执行顺序
总结摘要
Spring AOP Aspect 执行顺序
源码分析
Advisor 排序规则
- Aspect Bean 顺序,根据 Bean 的 Order 注解或其它 Order 标识排序。
- Aspect 注解顺序 Around.class,Before.class,After.class,AfterReturning.class,AfterThrowing.class。
- 方法名称。
实现
- org.springframework.aop.aspectj.annotation.ReflectiveAspectJAdvisorFactory#adviceMethodComparator 对 Aspect Bean 中的 Aspect 方法排序,首先根据注解,其次根据方法名称。
- org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator#sortAdvisors 对 Aspect Bean 排序,使用稳定的拓扑排序算法,基于规则 org.springframework.core.annotation.AnnotationAwareOrderComparator#INSTANCE。
验证
执行日志
|
|
LoggingAAspectSpringBean
LoggingBAspectSpringBean
org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator#sortAdvisors
(org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator#findEligibleAdvisors)
(org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#getAdvicesAndAdvisorsForBean)