Sub topic 12
Controlling the order of aspects
This chapter assumes you can write an @Aspect with advice and a pointcut, and that you know Spring wraps beans in proxies. When several aspects match the same method, the order in which they run affects the result. A security check should run before a timer starts, and a retry aspect should wrap a transaction aspect. This chapter shows how to set that order.
Precedence between aspects
Spring builds a chain of advice for each proxied method. By default, the order of aspects in that chain is undefined. To fix it, put @Order on the aspect class, or implement the Ordered interface.
A lower number means higher precedence. The aspect with the highest precedence runs first on the way in and last on the way out.
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Aspect
@Component
@Order(1)
class SecurityAspect {
@Around("execution(* com.example.service..*(..))")
public Object check(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("security: before");
try {
return pjp.proceed();
} finally {
System.out.println("security: after");
}
}
}
@Aspect
@Component
@Order(2)
class LoggingAspect {
@Around("execution(* com.example.service..*(..))")
public Object log(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("logging: before");
try {
return pjp.proceed();
} finally {
System.out.println("logging: after");
}
}
}Calling a service method prints security: before, logging: before, then the method's own output, then logging: after and security: after. The aspects nest like layers, and @Order(1) is the outermost layer.
Ordering inside one aspect
Advice methods inside a single aspect class follow a fixed rule. From Spring 5.2.7 onward, the order of precedence is @Around, @Before, @After, @AfterReturning, @AfterThrowing. On the way out, the order reverses, so @AfterReturning and @AfterThrowing run before @After.
Two advice methods of the same type in one aspect have no guaranteed order. If you need a guaranteed order, move them into separate aspects and give each an @Order value.
Common errors
- Putting
@Orderon an advice method. It has no effect. Only the aspect class is ordered. - Leaving out
@Order. Two aspects may happen to run in a stable order today and change after you add a bean. Order any aspects that must run in a set sequence. - Forgetting transactions.
@Transactionalis also advice. Its position is set by@EnableTransactionManagement(order = ...), which defaults to lowest precedence. A retry aspect that must wrap each transaction needs a higher precedence than the transaction advice.
Exercise
Aspect A has @Order(5) and aspect B has @Order(10). Both use @Around on the same method. Which runs first on the way in, and which finishes last?
Show answer
A runs first on the way in because the lower number has higher precedence. A also finishes last, because it is the outer layer around B.