import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class AppConfig { }
The @EnableCaching annotation tells Spring to look for caching annotations like @Cacheable and activate caching behavior on those methods. Without it, caching annotations are ignored.
The @EnableCaching annotation must be used on a class annotated with @Configuration to enable caching properly. @EnableCache does not exist and @EnableCaching does not take parameters.
Spring caching works through proxies. If a method annotated with @Cacheable is called from within the same class, the proxy is bypassed and caching does not occur.
@EnableCaching enables Spring to detect caching annotations and sets up a default cache manager if none is provided. It does not create caches automatically nor replace caching annotations.
The method is called with inputs 5, 5, 10, 5. The first call with 5 executes and increments count. The second call with 5 returns cached result, no increment. The call with 10 executes and increments count. The last call with 5 returns cached result again. So count increments twice.