Sub topic 6
Spring AOP versus AspectJ: what each can intercept
This chapter assumes you know the AOP vocabulary from the earlier chapters (aspect, join point, pointcut, advice, weaving). It also assumes you know that Spring AOP works by wrapping beans in JDK or CGLIB proxies. Here you will see how that design limits what Spring AOP can intercept, how AspectJ removes those limits, and how to choose between them.
Two tools that share a syntax
The names cause confusion because Spring AOP and AspectJ look alike. Spring AOP borrows AspectJ's annotations (@Aspect, @Around, @Pointcut) and its pointcut expression language. It does not use AspectJ's compiler or weaver. Spring only parses the expressions and then matches them against beans at runtime.
The two tools differ in how advice reaches your code:
- Spring AOP is proxy-based. At runtime, Spring replaces a bean with a proxy. Advice runs only when a caller invokes a method on that proxy.
- AspectJ is a full AOP implementation. It modifies bytecode, either at compile time or when classes load. The advice becomes part of the woven class, so no proxy is involved.
Everything else in this chapter follows from that difference.
What Spring AOP can intercept
Spring AOP supports one kind of join point: method execution on a Spring-managed bean, invoked from outside the bean through its proxy. That has several consequences:
- The target must be a bean in the container. Objects created with
neware not proxied. - Only calls that cross the proxy boundary are intercepted. A call from one method to another on
thisis not (the self-invocation problem, covered in its own chapter). - Methods that a proxy cannot override or implement are out of reach. These are
final,privateandstaticmethods. A JDK proxy also sees only the methods declared on its interfaces. - Constructors, field reads and writes, and static initializers are not join points.
Spring accepts only a subset of AspectJ's pointcut designators: execution, within, this, target, args, @target, @args, @within, @annotation, and Spring's own bean. If you use any other designator, Spring rejects the expression when the context starts.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class CallSiteAspect {
// 'call' is a valid AspectJ designator but not a Spring AOP one.
// Spring fails with: IllegalArgumentException: Unsupported pointcut primitive 'call'
@Before("call(* com.example.service.OrderService.place(..))")
public void beforeCall() {
System.out.println("never reached under Spring AOP");
}
}The failure is useful. Spring tells you at startup that the expression is outside what proxies can do, instead of silently ignoring it.
What AspectJ can intercept
AspectJ weaves advice into bytecode, so it can target many more join points. Here are the main ones:
| Join point | Designator | Spring AOP | AspectJ |
|---|---|---|---|
| Method execution | `execution` | Yes, on beans, through the proxy | Yes, on any class that is woven |
| Method call (at the call site) | `call` | No | Yes |
| Constructor execution | `execution(*.new(..))` | No | Yes |
| Object initialization | `initialization` | No | Yes |
| Field read | `get` | No | Yes |
| Field write | `set` | No | Yes |
| Static initializer | `staticinitialization` | No | Yes |
| Exception handler | `handler` | No | Yes |
| Calls from `this` to `this` | `execution` | No (no proxy involved) | Yes |
| Objects created with `new` | any | No | Yes |
Because the advice is compiled into the class itself, a self-invocation is still intercepted. The final and private restrictions that apply to proxies do not apply to woven code either.
The next example is an aspect in annotation style that intercepts writes to a field. No proxy can express this.
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class BalanceWriteAspect {
@Before("set(int com.example.Account.balance) && args(newValue)")
public void onWrite(JoinPoint jp, int newValue) {
System.out.println("writing balance = " + newValue
+ " at " + jp.getSourceLocation());
}
}This aspect does nothing until it is woven. You run the AspectJ compiler (ajc, usually through a build plugin) over your classes. Alternatively, you start the JVM with -javaagent:aspectjweaver.jar and list the aspect in META-INF/aop.xml for load-time weaving.
Choosing between them
Use these criteria:
- Start with Spring AOP. Logging, timing, security checks, auditing and transactions on service beans all fit proxies. You need no extra build step or JVM flag, and the behavior is easy to debug.
- Move to AspectJ when the join point is not a bean method call. Examples are field access, constructors,
new-created domain objects, andfinalorprivatemethods. - Move to AspectJ when self-invocation matters and you cannot restructure the code. Weaving removes the proxy boundary.
- Weigh the cost. Weaving adds build or startup complexity. Compile-time weaving needs
ajcin the build. Load-time weaving needs a Java agent and can slow startup. Proxies add a small cost per call, but they need no tooling.
Using both in one Spring application
Spring can host AspectJ aspects. For example, @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) switches @Transactional from proxies to woven advice, and it needs the spring-aspects module. Likewise, @EnableLoadTimeWeaving turns on load-time weaving, and @Configurable uses weaving to inject dependencies into objects created with new.
Exercise
An aspect uses @Before("set(* com.example.Account.balance)"). The application runs under Spring Boot with spring-boot-starter-aop and Account is a @Component. What happens at startup, and what would you change so the aspect works?
Show answer
Spring AOP does not support the set designator, so the context fails to start with an IllegalArgumentException about an unsupported pointcut primitive. Making Account a bean does not help, because proxies cannot intercept field writes. Weave the aspect with AspectJ, either at compile time with ajc or at load time with the aspectjweaver agent. Alternatively, change the design so state changes go through a setter method on a bean and use an execution pointcut on that method.