Backend Development 3 min read

Spring AOP Advice Execution Order Changes After Upgrading to Spring Boot 2.7.1

Upgrading to Spring Boot 2.7.1 (Spring Framework 5.3.21) alters the execution order of @Around, @Before, @After, @AfterReturning, and @AfterThrowing advice within the same @Aspect, causing ThreadLocal data loss and prompting a recommendation to consolidate logic into a single @Around advice.

Cognitive Technology Team
Cognitive Technology Team
Cognitive Technology Team
Spring AOP Advice Execution Order Changes After Upgrading to Spring Boot 2.7.1

Issue Overview

After upgrading the Spring Boot version to 2.7.1, which uses Spring Framework 5.3.21, the execution order of AOP advice annotations (@Around, @Before, @After, @AfterReturning, @AfterThrowing) inside the same @Aspect changed.

<spring-framework.version>5.3.21</spring-framework.version>

The new order is @Around, @Before, @After, @AfterReturning, @AfterThrowing , whereas older versions followed a different sequence. This change leads to two main problems:

The execution sequence of @Around, @Before, and @After is altered.

ThreadLocal variables set in @Before become unavailable when accessed inside @Around, resulting in data loss.

Official Explanation

The Spring documentation (see Spring AOP Advice Ordering ) notes that from version 5.2.7 onward, the order within a single @Aspect is fixed as mentioned above.

Reproduction Steps

Images in the original article illustrate the faulty execution order and the resulting output where ThreadLocal data is missing.

Recommended Solution

To avoid the complexities introduced by the changed order, it is advised to use only @Around advice in an @Aspect and move the logic originally placed in @Before and @After into the @Around method. This ensures the correct sequence: before → around (pre) → original method → around (post) → after .

Implementing this approach restores the expected business logic execution and prevents ThreadLocal information loss.

Conclusion

Since Spring 5.2.7, the advice execution order within a single @Aspect is @Around, @Before, @After, @AfterReturning, @AfterThrowing, which differs from earlier versions and can affect resources like ThreadLocal. Consolidating advice into a single @Around method simplifies development and avoids order‑related issues.

AOPSpring BootthreadlocalAspectJAdvice Order
Cognitive Technology Team
Written by

Cognitive Technology Team

Cognitive Technology Team regularly delivers the latest IT news, original content, programming tutorials and experience sharing, with daily perks awaiting you.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.