学习过程种遇到的一点疑惑在这里提个问答记录一下,看有没有人恰好知道的
首先声明下我的spring-aop版本:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.10</version>
</dependency>
在ReflectiveAspectJAdvisorFactory.getAdvisors中有这样一段代码:

通过注释可以看出,spring 5.2.7版本前后,spring 通知执行的顺序稍有不一样的地方,我主要的疑问就在这块了
// Prior to Spring Framework 5.2.7, advisors.size() was supplied as the declarationOrderInAspect
// to getAdvisor(...) to represent the "current position" in the declared methods list.
// However, since Java 7 the "current position" is not valid since the JDK no longer
// returns declared methods in the order in which they are declared in the source code.
// Thus, we now hard code the declarationOrderInAspect to 0 for all advice methods
// discovered via reflection in order to support reliable advice ordering across JVM launches.
// Specifically, a value of 0 aligns with the default value used in
// AspectJPrecedenceComparator.getAspectDeclarationOrder(Advisor).
这里我在贴下比较顺序的代码(AbstractAdvisorAutoProxyCreator.findEligibleAdvisors):

AspectJPrecedenceComparator.sortAdvisors

AspectJPrecedenceComparator.compare

上面的advisorComparator是判断Aspect切面是否有ordered注解,这里就不展开讲述了,主要看下面comparePrecedenceWithinAspect

可以看到5.2.7之后的版本,就是我上面贴的版本:adviceDeclarationOrderDelta是0,而5.2.7之前,则有区别。经过验证之后也会发现After、AfterReturning和AfterThrowing执行的顺序会不同。
我的主要疑问是上面那段注释,说是跟方法声明的顺序有关,但我调整了方法的顺序,执行顺序依然没有发生变化,原因应该是在ReflectiveAspectJAdvisorFactory.getAdvisorMethods方法里会对通知进行排序,如果是这个情况,我真的不明白这段注释的意思,跟JDK有什么关系,因为getAdvisorMethods(具体代码贴在后面)会按方法名称排序。对了我研究的是@Aspect注解的AOP实现,解析类主要是通过ReflectiveAspectJAdvisorFactory,如果有其他AOP实现,其内部调用getAdvisor方法前,是否存在不会对方法进行排序的情况就直接调用getAdvisor方法?如果是这种情况,麻烦贴出相应的源码
备注:在这里贴下getAdvisorMethods方法的实现:


看代码,应用了两个比较器,一个是按照
Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class
顺序进行排序,然后再按照方法名称进行排序