Quick revision
The short answer
We give each aspect class an order, either with the @Order annotation or by implementing the Ordered interface. A lower order value means higher precedence: that aspect runs first on the way into the method and last on the way out, so it wraps the others. Without an explicit order, the order between aspects in different classes is undefined, and two pieces of advice of the same type inside one aspect also have no defined order, so we split them into separate aspects when their order matters.
What to remember
In this answer, we saw that aspects form nested layers around a method and that their order decides which one is outermost. We set it with @Order on the aspect class or by implementing Ordered, and a lower value means higher precedence: first on the way in, last on the way out. Aspects without an order have an undefined relative order, and advice of the same type inside one aspect also has none, so when order matters we declare it explicitly or split the advice into separate aspects.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We give each aspect class an order, either with the @Order annotation or by implementing the Ordered interface. A lower order value means higher precedence: that aspect runs first on the way into the method and last on the way out, so it wraps the others. Without an explicit order, the order between aspects in different classes is undefined, and two pieces of advice of the same type inside one aspect also have no defined order, so we split them into separate aspects when their order matters.
Why this question is asked
As soon as an application has more than one aspect, say one that logs calls and one that checks permissions, the question of who runs first stops being academic. If logging runs first, we log calls that are then rejected; if security runs first, rejected calls never reach the log. Interviewers ask this to check that we know how Spring decides, which way the numbers go, and where the rules leave the order undefined. In this answer, we will recap how advice wraps a method, see how to set the order, explain why a lower value runs first and last, and look at the two places where Spring gives no guarantee.
1A Quick Recap of Advice Around a Method
An aspect is a class annotated with @Aspect that holds advice, the code to run, and pointcuts, expressions that select which methods the advice applies to. Spring AOP applies aspects through a proxy, a wrapper object that Spring creates around the bean. When another bean calls the method, it really calls the proxy, which runs a chain of interceptors, one per piece of advice, before and after calling the real method.
The easiest way to picture the chain is as nested layers, like the skins of an onion. The outermost layer starts first and finishes last. An @Around advice makes this visible: it runs code, calls proceed() to enter the next layer, and runs more code when that layer returns. So the question of aspect order is simply the question of which aspect becomes the outer layer.
2Setting the Order With @Order or Ordered
Spring offers two equivalent ways to give an aspect an order value. We can put @Order on the aspect class, or the class can implement the Ordered interface and return the value from getOrder(). Both are read when Spring builds the interceptor chain for a proxy.
Notice that the order goes on the aspect class, not on the advice method. Putting @Order on an individual @Around method has no effect on aspect precedence, which is a common mistake. The value is an int, so we can use any numbers, and leaving gaps such as 100, 200 and 300 makes it easy to slot a new aspect in later.
3Why a Lower Value Runs First and Last
Spring treats a lower order value as higher precedence, the same convention it uses everywhere Ordered appears, from filters to bean post processors. Ordered.HIGHEST_PRECEDENCE is Integer.MIN_VALUE and Ordered.LOWEST_PRECEDENCE is Integer.MAX_VALUE, which is also the default when no order is given.
For aspects, highest precedence means outermost. On the way in, the highest-precedence aspect's before advice runs first. On the way out, the same aspect's after advice runs last, because the inner layers must finish before control returns to it. The reason this matters is that the outer aspect sees everything the inner ones do: if the security aspect is outside, it can stop the call before logging even starts, and if a retry aspect is outside a transaction, each attempt gets a fresh transaction.
9 messages in order. Play them, or step through them one at a time.
The aspect with the lower order value is entered first and left last.
4When the Order Is Undefined
The first gap in the rules is between aspects that declare no order. Both get the default LOWEST_PRECEDENCE, and the Spring reference documentation says the order of advice from different aspects with the same precedence is undefined. In practice it often follows bean registration order, which depends on class path scanning and can change when a class is renamed or a module is added. So code that works by accident on one machine can behave differently after an unrelated change. Whenever two aspects can apply to the same method and their order affects the result, both should declare an order.
The second gap is inside a single aspect class. Since Spring Framework 5.2.7, advice methods in the same @Aspect class are ordered by advice type: @Around first, then @Before, @After, @AfterReturning and @AfterThrowing. But two pieces of advice of the same type in one class, for example two @Before methods, have no defined order, because Java reflection does not guarantee the order in which it returns a class's methods.
If the order between these two matters, we either merge them into one advice method that does both steps in the order we want, or we move one of them into its own aspect class with its own @Order. Splitting is the cleaner choice when the two concerns are really different, such as tenant checking and auditing.
5Follow-up questions
Does @Order on the @Component affect the order of the proxy chain or the order of beans in a List?
Both, in a sense. On an @Aspect class, Spring uses it to sort the advisors in each proxy's interceptor chain. The same annotation is also used when Spring injects a List of beans of one type, where it sorts the list. They are separate uses of the same convention, so the order of an aspect does not change when other beans are created.
How do built-in features such as @Transactional fit into this ordering?
They are advisors in the same chain. The transaction advisor uses LOWEST_PRECEDENCE by default, so it is usually the innermost layer, and we can change it with @EnableTransactionManagement(order = ...). Method security, caching and async each have their own order setting, so when combining them with our aspects we should look up their values rather than assume.
Can an aspect with a lower order skip the inner aspects completely?
Yes. An @Around advice that does not call proceed(), or throws before calling it, stops the chain there, so neither the inner aspects nor the target method run. This is exactly how an outer security aspect prevents a rejected call from being logged or executed.
6Conclusion
In this answer, we saw that aspects form nested layers around a method and that their order decides which one is outermost. We set it with @Order on the aspect class or by implementing Ordered, and a lower value means higher precedence: first on the way in, last on the way out. Aspects without an order have an undefined relative order, and advice of the same type inside one aspect also has none, so when order matters we declare it explicitly or split the advice into separate aspects.
Open with
To control the order of two aspects, I put @Order on each aspect class, or have the class implement Ordered and return a value from getOrder. The order goes on the class, not on the advice method.
Then, if they want more
A lower value means higher precedence, and for aspects that means the outer layer: its before logic runs first on the way into the method, and its after logic runs last on the way out, because the inner aspects have to finish before control returns to it.
So with security at order 1 and logging at order 2, I see security in, logging in, the method, logging out, security out. That also means the outer aspect can stop the call entirely by not calling proceed.
If I do not set an order, both aspects get the lowest precedence and the documentation says their relative order is undefined; it may follow bean registration order and change after an unrelated refactoring, so I always set it when order matters.
Inside one aspect class, Spring orders advice by type, around before before and so on, but two advice methods of the same type, such as two Before methods, have no defined order because reflection does not guarantee method order. If that order matters I merge them or split them into separate ordered aspects.
I also leave gaps in the numbers so new aspects can fit in later.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 5
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Core technologies
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you control the order of two aspects that apply to the same method, and what does...": https://scrivio-interview-library.netlify.app/spring-boot/aop/558343cc88/