Sub topic 15
What Proxies Cannot Intercept
Spring AOP works by wrapping a bean in a proxy. Advice runs only when a call goes through that proxy. This chapter lists the calls that skip it, and explains why each one does.
This chapter assumes you know core Java (interfaces, inheritance, annotations), basic Spring dependency injection, and the difference between JDK dynamic proxies and CGLIB proxies from earlier chapters.
The rule behind every limitation
A proxy is a separate object that stands in front of the target. It can only intercept what it can see and override. Three things follow from that:
- A call must reach the proxy object. If the caller holds a reference to the raw target, nothing is intercepted.
- The proxy must be able to override or implement the method. JDK proxies implement interfaces. CGLIB proxies subclass the target class.
- The target must be a Spring-managed bean, because only the container wraps objects in proxies.
Every case below breaks one of these three conditions.
Self-invocation
When a method calls another method on the same object, the call goes to this. In the target, this is the raw object, not the proxy. The call never crosses the proxy, so no advice applies.
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ReportService {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveReport(String name) {
// runs in its own transaction only when called through the proxy
}
public void saveAll(java.util.List<String> names) {
for (String name : names) {
saveReport(name); // self-invocation: calls 'this', not the proxy, so no advice applies
}
}
}An outside caller who calls saveReport directly gets a new transaction. The same method called from saveAll runs with whatever transaction saveAll has, or none.
Common fixes, from simplest to most invasive:
- Move the advised method to another bean and inject that bean. This is usually the cleanest design.
- Inject the bean's own proxy (for example through
ObjectProvider<ReportService>) and call through it. This works, but it makes the class aware of its proxy. - Switch to AspectJ weaving, which changes the bytecode so internal calls are advised too.
Final and private methods
CGLIB creates a subclass and overrides the target's methods. Java does not allow overriding some methods:
| Method kind | CGLIB can advise it? | What happens |
|---|---|---|
| public, non-final | Yes | The subclass override runs advice, then delegates. |
| protected or package-private | Usually yes, but Spring AOP does not guarantee advice for them | Behavior depends on the proxy type and pointcut. Treat them as unsupported. |
| private | No | A subclass cannot override it. Advice never runs. |
| final | No | The method cannot be overridden. The call runs on the proxy instance, not the target. |
| static | No | Static methods belong to the class, not an instance. |
Spring's documentation states that, with proxy-based AOP, only public method calls through the proxy are intercepted. Plan on that.
The final case has a subtle edge. The proxy is an instance of the subclass, but it was created without running the target's constructor in the usual way. Its fields are not populated, so a final method that reads a field can see null or a default value. Do not make methods final on beans that Spring proxies. Also note that a final class cannot be subclassed at all, so CGLIB fails to create the proxy and the application fails to start.
JDK proxies have a related limit. They implement only interfaces, so a method that is not declared on an interface cannot be intercepted through the proxy. A caller that needs the concrete class type also cannot receive a JDK proxy, because the proxy does not extend that class.
interface GreetingService {
String greet(String name);
}
class GreetingServiceImpl implements GreetingService {
public String greet(String name) {
return "Hello, " + name;
}
public String shout(String name) { // not on the interface
return greet(name).toUpperCase();
}
}If this bean is proxied with a JDK proxy, you can only call greet on the proxy. Injecting the bean as GreetingServiceImpl fails with a type mismatch, because the proxy is not an instance of that class. Spring Boot defaults to CGLIB (spring.aop.proxy-target-class=true), which avoids that particular failure.
Objects Spring does not manage
Only beans from the application context are wrapped. These are never advised:
- Objects created with
new, even inside a bean. - Objects created by libraries (for example JPA entities or objects deserialized from JSON).
- Beans created before the proxy-creating post-processor is ready, such as some infrastructure beans.
- The target itself when you hold a reference to it, for instance a bean method that returns
thisfrom a proxied bean.
The last case is easy to miss. If a method on a proxied bean returns this, the caller receives the raw target and later calls skip advice.
import org.springframework.stereotype.Component;
@Component
public class Registry {
public Registry self() {
return this; // leaks the unproxied target
}
}Constructors and field access are also out of reach. Spring AOP can intercept method execution only. It cannot advise field reads or writes, constructor calls, or static initializers. Those need AspectJ.
Calls made before the proxy exists
Lifecycle callbacks run on the raw object before proxy creation finishes. A @PostConstruct method that calls another method on the same bean is a self-invocation, so @Transactional and @Async on that second method do not apply. The same applies to constructor code.
Diagnosing a missing advice
Work through these checks in order:
- Is the class a Spring bean? Check that it is injected and not created with
new. - Does the call come from another bean, through an injected reference?
- Is the method public and non-final, and is the class non-final?
- Does the pointcut match? Log
AopUtils.isAopProxy(bean)to confirm a proxy exists at all. - Is
@EnableAspectJAutoProxy(orspring-boot-starter-aop) active?
import org.springframework.aop.support.AopUtils;
import org.springframework.stereotype.Component;
@Component
class ProxyInspector {
ProxyInspector(ReportService service) {
System.out.println("is proxy: " + AopUtils.isAopProxy(service));
System.out.println("cglib: " + AopUtils.isCglibProxy(service));
System.out.println("jdk: " + AopUtils.isJdkDynamicProxy(service));
}
}When these limits matter enough to switch
If self-invocation, private methods, or non-managed objects are central to your design, AspectJ weaving removes the proxy from the picture by changing the bytecode. It costs a build plugin or a Java agent. Most applications are better served by restructuring code so advised calls cross bean boundaries.
Exercise
A @Service has a public method process() that calls its own public method audit(), which is annotated with a custom @Audited annotation handled by an @Around aspect. External callers call process(). Does the audit advice run, and what are two ways to fix it?
Show answer
No. process() calls audit() on this, the raw target, so the call never passes through the proxy. Fixes: move audit() to a separate bean and inject it, or inject the bean's own proxy and call through it. AspectJ weaving would also work but is heavier.
Exercise
You mark a method final in a CGLIB-proxied bean and the advice stops running. Why, and what extra risk does the call carry?
Show answer
CGLIB cannot override a final method, so the call is not intercepted. It runs on the proxy instance, whose own fields were not populated by the normal constructor path, so the method may see null or default field values.