Sub topic 15
What proxies cannot intercept
This chapter assumes you know how Spring creates JDK and CGLIB proxies and how self-invocation skips advice. Here we list the other cases where advice silently does not run.
The rule behind every limit
Spring AOP applies advice only when a call passes through the proxy and the proxy can override the method. Anything that bypasses the proxy, or that a proxy cannot override, runs without advice and without any error message.
Final and private methods
A CGLIB proxy is a subclass of your bean. A subclass cannot override a final or private method, so calls to those methods never reach the advice. A final method is the worse case. It runs on the proxy instance itself, whose fields may not be initialized, because Spring creates the proxy without calling your constructor.
A final class cannot be subclassed at all. Spring fails at startup when it needs a CGLIB proxy for one. JDK proxies have a different limit: they intercept only methods declared on the interfaces.
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
class PriceService {
public double quote(String item) {
return base(item) * 1.2; // advised: public, overridable
}
public final double discount(String item) {
return 0.9; // not advised: final
}
private double base(String item) {
return 10.0; // not advised: private
}
}
@Aspect
@Component
class LogAspect {
@Before("execution(* PriceService.*(..))")
public void log() {
System.out.println("advice ran");
}
}With this code, quote prints "advice ran" when called on the injected bean. discount does not, and the call from quote to base does not either.
Objects Spring does not manage
Only beans in the container get proxies. An object you create with new is the plain target, so no advice applies to it:
PriceService manual = new PriceService();
manual.quote("pen"); // no "advice ran"The same applies to objects built by a library, such as entities, DTOs and deserialized objects. Beans created outside the container's normal lifecycle, such as an instance you register as a singleton by hand after startup, may also miss proxy creation.
Other things that are never join points
- Static methods belong to the class, not to an instance.
- Field reads and writes, and constructor calls, are not join points in Spring AOP. It supports method execution only.
- Methods called on
this, covered in the self-invocation chapter.
When you need to advise any of these cases, you have two options. You can move the logic to a separate public bean method. Or you can use AspectJ weaving, which changes the bytecode instead of wrapping the bean.
Exercise
In the example, PriceService is injected into a controller, and the controller calls quote("pen"). Which of quote, discount and base run with the advice, and why?
Show answer
Only quote is advised. The controller calls it through the proxy, and it is public and overridable. discount is final, so the CGLIB subclass cannot override it. base is private and is called from inside the target, so it never goes through the proxy.