The @Order annotation sets the precedence of Spring beans that take part in an ordered sequence, and in Spring AOP it decides which aspect runs first when several aspects match the same method. A lower value means higher precedence: @Order(1) runs before @Order(2). Without it, the order between aspects is undefined, so logging, security and transaction aspects can interleave unpredictably.
The order applies at the aspect class level. You can also implement the Ordered interface instead of using the annotation.
@Order takes an int. The default is Ordered.LOWEST_PRECEDENCE (Integer.MAX_VALUE), the least important position. Ordered.HIGHEST_PRECEDENCE is Integer.MIN_VALUE. Spring sorts the aspects by value and builds the interceptor chain from them.
Precedence is symmetrical, like layers around a method:
So with aspects A (@Order(1)) and B (@Order(2)), the flow is A before, B before, the method, B after, A after.
Calling an OrderService method prints:
If you swapped the values, logging would wrap security. That matters: you normally want authorisation to run before expensive or sensitive work, and logging to wrap everything including security failures.
The interface is useful when the order must be computed or shared as a constant. When a class has both, Ordered takes priority over the annotation.
@Order on the aspect class. Placing it on an individual advice method has no effect on aspect precedence.@Order ranks aspects, not the matched methods.Transaction advice has its own order, set with @EnableTransactionManagement(order = ...); its default is lowest precedence. Caching has the same attribute on @EnableCaching(order = ...). If your aspect must run inside a transaction (or outside it), give it an order above or below that value. Retry aspects are a classic case: a retry must wrap the transaction so each attempt gets a fresh one.
@Order is a general ordering tool, not only for AOP:
| Use | Effect |
|---|---|
Injected List<T> or array of beans | The collection is sorted by order |
@Component filters and interceptors in Spring Boot | Servlet Filter beans are ordered by @Order |
CommandLineRunner and ApplicationRunner beans | Run in order |
@EventListener methods | Listeners for the same event run in order |
Note that @Order does not control bean creation or dependency order. Use @DependsOn for that.
The lower number has higher precedence and runs first on the way in. After advice runs in the reverse order on the way out.
Ordered.LOWEST_PRECEDENCE, which places the aspect after all explicitly ordered aspects.
Not to rank aspects. Order applies to the aspect class, and you should split advice into separate aspects if you need control between them.
Transaction advice has its own order setting on @EnableTransactionManagement. Compare it with your aspect's order to decide whether your aspect runs inside or outside the transaction.
@Order (or Ordered) gives deterministic precedence to Spring AOP aspects: lower value, higher precedence, first in and last out. Put it on aspect classes, leave gaps between values, and remember the separate order settings for transactions and caching. Outside AOP the same annotation sorts injected lists, filters and runners.