Quick revision
The short answer
pricesFor() calls price() through this::price, which is a plain method call on the real object, so it never passes through the caching proxy and @Cacheable never runs. The usual fix is to move price() into a separate bean and inject it; the alternatives are injecting the bean's own proxy lazily or switching caching to AspectJ weaving. The same rule applies to @Transactional, @Async and every other proxy-based annotation.
What to remember
In this answer, we saw that pricesFor() calls price() through this::price, which is a call on the real object and never reaches the caching proxy, so every element goes to the database. The usual fix is to move the cached method into its own bean and inject it; self-injection with @Lazy, AspectJ weaving, or direct use of the Cache API also work, each with its own cost. The same self-invocation rule silently disables @Transactional, @Async and security annotations, and a test that counts repository calls for two identical requests is the cheapest way to keep the bug from coming back.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
pricesFor() calls price() through this::price, which is a plain method call on the real object, so it never passes through the caching proxy and @Cacheable never runs. The usual fix is to move price() into a separate bean and inject it; the alternatives are injecting the bean's own proxy lazily or switching caching to AspectJ weaving. The same rule applies to @Transactional, @Async and every other proxy-based annotation.
Why this question is asked
Few bugs are as frustrating as an annotation that is clearly there and clearly does nothing. This one is especially sneaky because the method reference this::price looks like it should be the same as calling the method from outside. Interviewers use it to check whether we understand how Spring adds behaviour to beans, because the explanation is the same for caching, transactions, async execution and security checks. In this answer, we will recap how @Cacheable is applied, reproduce the problem, see exactly why the call skips the proxy, go through the fixes with their costs, and write a test that would have caught it.
1A Quick Recap of How @Cacheable Is Applied
Spring does not change our PriceService class to add caching. When the application starts, it creates a proxy around the bean: a wrapper object, generated at runtime, that looks like PriceService from the outside. Every other bean that injects PriceService really receives the proxy. When another bean calls price(id), the call reaches the proxy first, the proxy's CacheInterceptor looks in the "prices" cache, and only on a miss does it pass the call on to our real object.
So the caching logic lives in the proxy, not in our class. That one fact explains the whole bug.
2Reproducing the Problem
We can make the problem visible by logging inside price() and calling pricesFor() twice with the same ids.
Notice that the last two lines behave correctly. When we call price() from outside the bean, the cache works. Only the calls that start inside pricesFor() miss every time. The annotation is fine; the path the call takes is the problem.
3Why the Call Skips the Proxy
The controller calls pricesFor() on the proxy. pricesFor() has no caching annotation, so the proxy forwards the call straight to the real PriceService object. From that moment we are running inside the real object.
The method reference this::price is bound to this, and this is the real object, not the proxy. Each map() step is therefore an ordinary Java method call on the real object. It goes straight to price() and repo.load(id) without ever meeting the CacheInterceptor. Writing price(id) without this, or using a lambda, changes nothing, because all of them mean the same object. We call this self-invocation.
7 messages in order. Play them, or step through them one at a time.
The cache lives in the proxy, and self-invocation stays inside the real object.
4Fix 1: Move the Cached Method Into Its Own Bean
The usual fix is to put the cached lookup in a separate bean and inject it. The call then leaves PriceService and enters the new bean through its proxy.
Now lookup::price is bound to the injected proxy, so every element passes through the CacheInterceptor. This also improves the design: the cached lookup is a small, clearly named component, and it is easy to test on its own.
5Other Fixes and Their Costs
The second option is self-injection: PriceService injects itself, marked @Lazy so Spring injects a lazy-resolving proxy and avoids a circular reference problem at startup, and then calls self.price(id). It works, but it reads oddly and hides the real reason, so a future developer may "clean it up" back into this.price.
The third option is AspectJ weaving with @EnableCaching(mode = AdviceMode.ASPECTJ), plus spring-aspects and load-time or compile-time weaving. The caching code is then woven into the class bytecode, so self-invocation is cached too. It is the most powerful fix and the heaviest, because it changes the build or the JVM startup for the whole application. A fourth, simple option is to call the Cache API directly with an injected CacheManager inside pricesFor(), which is explicit but gives up the annotation's convenience.
Whichever we choose, remember that the same rule applies to every proxy-based feature. A @Transactional method called from the same class runs without a transaction, an @Async method called from the same class runs synchronously, and a @PreAuthorize method called from the same class skips its security check. The last one is a security bug, not just a performance bug.
6A Test That Catches the Regression
Caching bugs are silent: the code returns the right answer, only slower. So we test the behaviour we actually want, which is that the repository is called once for two identical requests.
This test fails against the original code and passes after any of the fixes, so it protects the behaviour rather than one particular implementation.
7Follow-up questions
Would making price() private or final change anything?
No, it would not fix the problem, and it can make things worse. Self-invocation skips the proxy whatever the method's modifiers are. On top of that, a CGLIB proxy works by overriding methods in a generated subclass, and private and final methods cannot be overridden, so even calls from outside would no longer be intercepted. A final method called on a CGLIB proxy runs on the proxy instance itself, whose fields were never injected, so it can even fail with a NullPointerException on repo. Keep proxied methods public and non-final.
Why does the method reference this::price not count as an outside call?
A method reference captures its receiver when it is created. this::price captures the real object, because inside pricesFor() this is the real object. lookup::price, in contrast, captures the injected proxy. The reference is just a shorter way of writing a call, so it follows exactly the same rules as the call it stands for.
Could @Cacheable on pricesFor() itself be a fix?
It would cache whole lists keyed by the list of ids, which is a different cache. Requests for [1, 2] and [2, 1] would be separate entries, and a change to price 1 would need eviction of every list containing it. It can be useful for a hot, repeated request, but it does not fix per-price caching, and it often stores the same data many times.
8Conclusion
In this answer, we saw that pricesFor() calls price() through this::price, which is a call on the real object and never reaches the caching proxy, so every element goes to the database. The usual fix is to move the cached method into its own bean and inject it; self-injection with @Lazy, AspectJ weaving, or direct use of the Cache API also work, each with its own cost. The same self-invocation rule silently disables @Transactional, @Async and security annotations, and a test that counts repository calls for two identical requests is the cheapest way to keep the bug from coming back.
Open with
The problem is self-invocation. Spring implements @Cacheable with a proxy around PriceService, and other beans receive that proxy. When the controller calls pricesFor, the proxy forwards it to the real object, because pricesFor has no caching annotation.
Then, if they want more
Inside, this::price is bound to this, which is the real object, so each call to price is a plain Java call that never passes through the CacheInterceptor. That is why every call hits the database, while calling price directly from another bean is cached.
The cleanest fix is to move price into its own bean, say PriceLookup, inject it, and map with lookup::price, so the calls go through that bean's proxy. Alternatives are injecting the bean into itself with @Lazy and calling through that reference, or switching to AspectJ mode with @EnableCaching(mode = AdviceMode.ASPECTJ) and weaving, which also covers self-invocation but complicates the build.
I would point out that the same rule breaks @Transactional, @Async and @PreAuthorize, and the last one is a security issue. Making price private or final does not help; CGLIB cannot override those, so it gets worse. Finally, I would add a test that calls pricesFor twice with the same id and verifies the repository was called once.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Cache abstraction
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A developer reports that caching does not work. The class is:": https://scrivio-interview-library.netlify.app/databases/caching/3196498c6c/