Sub topic 6
Spring AOP versus AspectJ: what each can intercept
This chapter assumes you know Java interfaces, inheritance and annotations, and basic Spring beans. It also assumes you have read the earlier chapters on AOP terms and the proxy pattern.
Two tools, one vocabulary
Spring AOP and AspectJ both use the same terms: aspect, pointcut, advice and join point. They differ in how advice reaches your code.
- Spring AOP wraps Spring beans in proxies at runtime. A call is intercepted only if it passes through the proxy.
- AspectJ is a full AOP language and weaver. It changes the bytecode itself, either when you compile or when classes load. Advice then lives inside the woven class, so no proxy is needed.
Spring can reuse AspectJ's annotations (@Aspect, @Around) and its pointcut expression language. With proxy-based Spring AOP, the AspectJ library only parses those expressions. It does not weave anything.
What each can intercept
The proxy model limits Spring AOP to one kind of join point: method execution on Spring beans, called from outside the bean.
| Capability | Spring AOP | AspectJ weaving |
|---|---|---|
| Public method calls on beans | Yes | Yes |
| Protected and package-private methods | Limited, depends on proxy type | Yes |
| Private and final methods | No | Yes |
| Calls from one method to another in the same object | No (self-invocation) | Yes |
| Objects created with `new` | No | Yes |
| Constructors, field reads and writes | No | Yes |
| Static methods | No | Yes |
| Setup | Annotation and a bean | Weaver, compiler plugin or Java agent |
The first rows follow from the proxy mechanism. A proxy can only override or implement what callers invoke on it. Anything that never goes through the proxy is invisible to Spring AOP.
The same aspect, two behaviors
This aspect is valid for both tools.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void log() {
System.out.println("method called");
}
}Under Spring AOP, register it as a bean. It then runs only for calls that reach a service bean through its proxy. Under AspectJ weaving, the same pointcut also matches private methods, internal calls and objects built with new, because the advice is part of the class.
Choosing between them
Start with Spring AOP. It needs no build changes, and it covers logging, timing, security checks and transactions on beans. Move to AspectJ when you need to advise objects Spring does not manage, internal calls, private methods or field access. Weaving adds build or startup configuration, so use it only when the proxy limits block you.
Exercise
A class has a public method process() that calls a private method validate(). You want advice on validate(). Does Spring AOP with proxies work? Which option would?
Show answer
No. The call to validate() is internal and the method is private, so it never passes through the proxy. AspectJ compile-time or load-time weaving can advise it because the advice is woven into the class bytecode.