Sub topic 14
Why calls inside the same class skip advice
This chapter assumes you know how Spring wraps beans in JDK or CGLIB proxies and how @Transactional and @Aspect are declared. Here you will see why a call from one method to another in the same class bypasses the proxy, and how to fix it.
The proxy wraps the bean, not the class
Spring AOP adds behavior by placing a proxy in front of your bean. Other beans receive the proxy, so their calls pass through the advice before reaching the target. The target object does not know the proxy exists. Inside a method, this is the raw target, not the proxy.
When saveAll calls saveReport(name), Java compiles that as this.saveReport(name). The call goes straight to the target and the proxy never sees it.
A concrete failure
import java.util.List;
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) {
// expected: a new transaction for each report
}
public void saveAll(List<String> names) {
for (String name : names) {
saveReport(name); // self-invocation: no advice applies
}
}
}REQUIRES_NEW is silently ignored. There is no error or warning. The same applies to any advice, such as your own @Around timing aspect, on a method called this way.
Fixes
- Move the method to another bean. This is the cleanest option, because the call then crosses a bean boundary and goes through a proxy.
- Call through the proxy. Inject the bean into itself (with
@Lazyto avoid a circular reference), or useAopContext.currentProxy()after setting@EnableAspectJAutoProxy(exposeProxy = true). Both couple your code to Spring. - Use AspectJ weaving. Weaving changes the bytecode itself, so internal calls are advised too.
The first fix looks like this:
import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
class ReportWriter {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveReport(String name) {
// now runs in its own transaction
}
}
@Service
public class ReportService {
private final ReportWriter writer;
public ReportService(ReportWriter writer) {
this.writer = writer;
}
public void saveAll(List<String> names) {
for (String name : names) {
writer.saveReport(name); // goes through the ReportWriter proxy
}
}
}How to confirm the cause
Log AopUtils.isAopProxy(this) inside the method. It returns false there, because this is the target. In a bean that received the injected dependency, it returns true.
Exercise
A @Service has a public method a() with @Around advice from a timing aspect, and a public method b() that calls a(). A controller calls b(). Is a() timed? What is the simplest fix if you want it timed?
Show answer
No. The controller's call to b() goes through the proxy, but b() calls a() on this, which is the raw target, so the advice for a() is skipped. The simplest fix is to move a() into a separate bean and inject that bean into the service, so the call passes through that bean's proxy.