Annotating interfaces or interface methods with @Cache*
annotations is not recommended by the official Spring documentation:
Spring recommends that you only annotate concrete classes (and methods of concrete classes) with the @Cache* annotations, as opposed to annotating interfaces. You certainly can place an @Cache* annotation on an interface (or an interface method), but this works only if you use the proxy mode (mode="proxy"). If you use the weaving-based aspect (mode="aspectj"), the caching settings are not recognized on interface-level declarations by the weaving infrastructure.
Also, when a method is annotated as cacheable inside an interface, if two different implementations of that method exist, the first one to be
invoked will populate the cache. Subsequent calls will always return the cached value, even if it’s the other implementation being called.
What is the potential impact?
- Confusing Code: Developers may mistakenly believe that caching is in effect, leading to confusion and incorrect assumptions
about application performance.
- Unreliable Code: Annotating interface methods as
@Cacheable
hides the cache name from the implementing classes,
making it hard to detect where a conflict of names might occur, causing unexpected results at runtime.
This rule raises an issue when an interface or an interface method is annotated with a @Cache*
annotation.