Skip to content

Commit 0998bd4

Browse files
committed
Implement reliable advice invocation order within an @aspect
The AspectJPrecedenceComparator was designed to mimic the precedence order enforced by the AspectJ compiler with regard to multiple 'after' methods defined within the same aspect whose pointcuts match the same joinpoint. Specifically, if an aspect declares multiple @after, @AfterReturning, or @AfterThrowing advice methods whose pointcuts match the same joinpoint, such 'after' advice methods should be invoked in the reverse order in which they are declared in the source code. When the AspectJPrecedenceComparator was introduced in Spring Framework 2.0, it achieved its goal of mimicking the AspectJ compiler since the JDK at that time (i.e., Java 5) ensured that an invocation of Class#geDeclaredMethods() returned an array of methods that matched the order of declaration in the source code. However, Java 7 removed this guarantee. Consequently, in Java 7 or higher, AspectJPrecedenceComparator no longer works as it is documented or as it was designed when sorting advice methods in a single @aspect class. Note, however, that AspectJPrecedenceComparator continues to work as documented and designed when sorting advice configured via the <aop:aspect> XML namespace element. PR gh-24673 highlights a use case where AspectJPrecedenceComparator fails to assign the highest precedence to an @after advice method declared last in the source code. Note that an @after advice method with a precedence higher than @AfterReturning and @AfterThrowing advice methods in the same aspect will effectively be invoked last due to the try-finally implementation in AspectJAfterAdvice.invoke() which invokes proceed() in the try-block and invokeAdviceMethod() in the finally-block. Since Spring cannot reliably determine the source code declaration order of annotated advice methods without using ASM to analyze the byte code, this commit introduces reliable invocation order for advice methods declared within a single @aspect. Specifically, the getAdvisors(...) method in ReflectiveAspectJAdvisorFactory now hard codes the declarationOrderInAspect to `0` instead of using the index of the current advice method. This is necessary since the index no longer has any correlation to the method declaration order in the source code. The result is that all advice methods discovered via reflection will now be sorted only according to the precedence rules defined in the ReflectiveAspectJAdvisorFactory.METHOD_COMPARATOR. Specifically, advice methods within a single @aspect will be sorted in the following order (with @after advice methods effectively invoked after @AfterReturning and @AfterThrowing advice methods): @around, @before, @after, @AfterReturning, @AfterThrowing. The modified assertions in AspectJAutoProxyAdviceOrderIntegrationTests demonstrate the concrete effects of this change. Closes gh-25186
1 parent d6525cf commit 0998bd4

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

integration-tests/src/test/java/org/springframework/aop/framework/autoproxy/AspectJAutoProxyAdviceOrderIntegrationTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ class AspectJAutoProxyAdviceOrderIntegrationTests {
5959
class AfterAdviceFirstTests {
6060

6161
@Test
62-
void afterAdviceIsNotInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceFirstAspect aspect) throws Exception {
62+
void afterAdviceIsInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceFirstAspect aspect) throws Exception {
6363
assertThat(aspect.invocations).isEmpty();
6464
assertThat(echo.echo(42)).isEqualTo(42);
65-
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after returning");
65+
assertThat(aspect.invocations).containsExactly("around - start", "before", "after returning", "after", "around - end");
6666

6767
aspect.invocations.clear();
6868
assertThatExceptionOfType(Exception.class).isThrownBy(
6969
() -> echo.echo(new Exception()));
70-
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after throwing");
70+
assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end");
7171
}
7272
}
7373

@@ -78,7 +78,7 @@ void afterAdviceIsNotInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceFir
7878
* in its source code.
7979
*
8080
* <p>On Java versions prior to JDK 7, we would have expected the {@code @After}
81-
* advice method to be invoked after {@code @AfterThrowing} and
81+
* advice method to be invoked before {@code @AfterThrowing} and
8282
* {@code @AfterReturning} advice methods due to the AspectJ precedence
8383
* rules implemented in
8484
* {@link org.springframework.aop.aspectj.autoproxy.AspectJPrecedenceComparator}.
@@ -89,15 +89,15 @@ void afterAdviceIsNotInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceFir
8989
class AfterAdviceLastTests {
9090

9191
@Test
92-
void afterAdviceIsNotInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceLastAspect aspect) throws Exception {
92+
void afterAdviceIsInvokedLast(@Autowired Echo echo, @Autowired AfterAdviceLastAspect aspect) throws Exception {
9393
assertThat(aspect.invocations).isEmpty();
9494
assertThat(echo.echo(42)).isEqualTo(42);
95-
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after returning");
95+
assertThat(aspect.invocations).containsExactly("around - start", "before", "after returning", "after", "around - end");
9696

9797
aspect.invocations.clear();
9898
assertThatExceptionOfType(Exception.class).isThrownBy(
9999
() -> echo.echo(new Exception()));
100-
assertThat(aspect.invocations).containsExactly("around - start", "before", "around - end", "after", "after throwing");
100+
assertThat(aspect.invocations).containsExactly("around - start", "before", "after throwing", "after", "around - end");
101101
}
102102
}
103103

spring-aop/src/main/java/org/springframework/aop/aspectj/annotation/ReflectiveAspectJAdvisorFactory.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
* @author Juergen Hoeller
6565
* @author Ramnivas Laddad
6666
* @author Phillip Webb
67+
* @author Sam Brannen
6768
* @since 2.0
6869
*/
6970
@SuppressWarnings("serial")
@@ -72,6 +73,11 @@ public class ReflectiveAspectJAdvisorFactory extends AbstractAspectJAdvisorFacto
7273
private static final Comparator<Method> METHOD_COMPARATOR;
7374

7475
static {
76+
// Note: although @After is ordered before @AfterReturning and @AfterThrowing,
77+
// an @After advice method will actually be invoked after @AfterReturning and
78+
// @AfterThrowing methods due to the fact that AspectJAfterAdvice.invoke(MethodInvocation)
79+
// invokes proceed() in a `try` block and only invokes the @After advice method
80+
// in a corresponding `finally` block.
7581
Comparator<Method> adviceKindComparator = new ConvertingComparator<>(
7682
new InstanceComparator<>(
7783
Around.class, Before.class, After.class, AfterReturning.class, AfterThrowing.class),
@@ -122,7 +128,15 @@ public List<Advisor> getAdvisors(MetadataAwareAspectInstanceFactory aspectInstan
122128

123129
List<Advisor> advisors = new ArrayList<>();
124130
for (Method method : getAdvisorMethods(aspectClass)) {
125-
Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, advisors.size(), aspectName);
131+
// Prior to Spring Framework 5.2.7, advisors.size() was supplied as the declarationOrderInAspect
132+
// to getAdvisor(...) to represent the "current position" in the declared methods list.
133+
// However, since Java 7 the "current position" is not valid since the JDK no longer
134+
// returns declared methods in the order in which they are declared in the source code.
135+
// Thus, we now hard code the declarationOrderInAspect to 0 for all advice methods
136+
// discovered via reflection in order to support reliable advice ordering across JVM launches.
137+
// Specifically, a value of 0 aligns with the default value used in
138+
// AspectJPrecedenceComparator.getAspectDeclarationOrder(Advisor).
139+
Advisor advisor = getAdvisor(method, lazySingletonAspectInstanceFactory, 0, aspectName);
126140
if (advisor != null) {
127141
advisors.add(advisor);
128142
}

0 commit comments

Comments
 (0)