Spring Boot 2.7.1 Upgrade Alters AOP Advice Execution Order and Causes ThreadLocal Loss
After upgrading to Spring Boot 2.7.1 (Spring Framework 5.3.21), the execution order of @Around, @Before, and @After advice changes, leading to ThreadLocal data loss inside @Around, and the article explains the cause, reproduces the issue, and recommends using only @Around for reliable logic.
Fault Overview
After upgrading Spring Boot to version 2.7.1, which uses Spring Framework 5.3.21, the execution order of AOP advices (@Around, @Before, @After) changes, and ThreadLocal variables accessed inside @Around become unavailable.
<spring-framework.version>5.3.21</spring-framework.version>Official Explanation
According to the Spring documentation, since version 5.2.7 the execution order within the same @Aspect is:
@Around, @Before, @After, @AfterReturning, @AfterThrowing . This differs from older versions where the order was different, which can affect resources such as ThreadLocal.
Fault Reproduction
Example screenshots (original behavior) show the expected order and output.
Output after the upgrade shows that @Around executes before @Before, and the ThreadLocal set in @Before is not visible inside @Around.
The original logic placed the ThreadLocal initialization in @Before (simulating a filter or HandlerInterceptor), reporting in @Around, and cleanup in @After. After the version change, @After runs immediately after the original method inside @Around, so the subsequent @Around code cannot access the ThreadLocal.
Recommendation
To avoid complex ordering issues, it is advised to use only @Around within an @Aspect and move the logic of @Before and @After into the @Around advice.
Resulting execution order matches the business requirement:
before → around (pre) → original method → around (post) → after
Conclusion
Since Spring Framework 5.2.7, the advice execution order inside a single @Aspect is @Around, @Before, @After, @AfterReturning, @AfterThrowing, which differs from older versions and can cause ThreadLocal loss. Using only @Around simplifies the flow and prevents such issues.
Cognitive Technology Team
Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.