Sub topic 8
Proxy behavior and the self-invocation trap
This chapter assumes you know Spring beans and dependency injection, and that you have already used @Cacheable on a service method. You should also have @EnableCaching in place.
How caching is applied
Spring does not change your class. When caching is enabled, Spring wraps each bean that has caching annotations in a proxy. A proxy is an object that stands in front of the real bean and has the same public methods. When another bean calls a method, the call goes to the proxy first. The proxy checks the cache. On a hit it returns the stored value. On a miss it calls your real method and stores the result.
The caching logic lives only in the proxy. Code that reaches the real object without going through the proxy gets no caching.
The self-invocation trap
Inside a method, this refers to the real object, not the proxy. A call such as load("daily") is really this.load("daily"). It skips the proxy, so the cache advice never runs.
@Service
public class ReportService {
public String latestDailyReport() {
// Self-invocation: this call does not go through the Spring proxy,
// so the @Cacheable advice on load() is never applied.
return load("daily");
}
@Cacheable("reports")
public String load(String name) {
return "report:" + name;
}
}If another bean calls reportService.load("daily"), the result is cached. If it calls latestDailyReport(), load runs every time. Nothing fails and no warning appears. The only sign is that the cache stays empty or the method keeps running.
Other proxy limits
- By default, only
publicmethods called from outside the bean are intercepted. Annotations onprivateor package-private methods are ignored in the default proxy mode. - Annotations on methods of objects you create yourself with
newhave no effect, because Spring never proxied them. finalclasses and methods cannot be subclassed. If Spring uses class-based (CGLIB) proxies, caching on those methods will not work.- Annotate the concrete class or its methods. Annotations placed on interfaces only work reliably with interface-based proxies.
Fixing self-invocation
The simplest fix is to move the cached method into a separate bean. The call then crosses a bean boundary and goes through the proxy.
@Service
public class ReportLoader {
@Cacheable("reports")
public String load(String name) {
return "report:" + name;
}
}
@Service
public class ReportService {
private final ReportLoader loader;
public ReportService(ReportLoader loader) {
this.loader = loader;
}
public String latestDailyReport() {
return loader.load("daily");
}
}Other options exist. You can inject the bean into itself, which is awkward and easy to break. You can switch to AspectJ mode with @EnableCaching(mode = AdviceMode.ASPECTJ), which weaves the advice into the bytecode and does catch self-calls, but it needs extra build setup. Splitting the class is usually the better choice, because it also makes the cache boundary visible in the design.
Checking that the proxy is in use
Write a test that calls the method twice through the injected bean and verifies that the underlying work happened once, or that the entry exists in the CacheManager. A later chapter covers testing in detail.
Exercise
A service has a public method getPrice(Long id) that calls findProduct(id) on the same class. findProduct is annotated with @Cacheable("products"). Callers use getPrice, and the database is still queried on every call. Why, and what is the simplest fix?
Show answer
getPrice calls findProduct through this, which is the real object and not the Spring proxy, so the cache advice is skipped. The simplest fix is to move findProduct into a separate bean and inject that bean into the service, so the call goes through the proxy.