Overview
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 invocations 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.
PR #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(MethodInvocation)
which invokes proceed()
in the try-block and invokeAdviceMethod()
in the finally-block.
Proposal
Since Spring cannot reliably determine the source code declaration order of such advice methods without using ASM to analyze the byte code, we have decided to implement reliable invocation order for 'after' advice methods declared within a single @Aspect
.
Related Issues
-
24673
-
25205
Deliverables
- [x] Implement reliable invocation order for
@After
,@AfterReturning
, and@AfterThrowing
advice methods within a single@Aspect
class, by ensuring that@AfterReturning
and@AfterThrowing
methods are always invoked before any@After
methods within the same@Aspect
class.