Annotation Type EnableCaching
@Target(TYPE) @Retention(RUNTIME) @Documented @Import(CachingConfigurationSelector.class) public @interface EnableCaching
Enables Spring's annotation-driven cache management capability, similar to the support found in Spring's<cache:*>XML namespace. To be used together with @Configurationclasses as follows:@Configuration @EnableCaching public class AppConfig { @Bean public MyService myService() { // configure and return a class having @Cacheable methods return new MyService(); } @Bean public CacheManager cacheManager() { // configure and return an implementation of Spring's CacheManager SPI SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default"))); return cacheManager; } }For reference, the example above can be compared to the following Spring XML configuration:
In both of the scenarios above,<beans> <cache:annotation-driven/> <bean id="myService" class="com.foo.MyService"/> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> <property name="name" value="default"/> </bean> </set> </property> </bean> </beans>@EnableCachingand<cache:annotation-driven/>are responsible for registering the necessary Spring components that power annotation-driven cache management, such as theCacheInterceptorand the proxy- or AspectJ-based advice that weaves the interceptor into the call stack when@Cacheablemethods are invoked.If the JSR-107 API and Spring's JCache implementation are present, the necessary components to manage standard cache annotations are also registered. This creates the proxy- or AspectJ-based advice that weaves the interceptor into the call stack when methods annotated with
CacheResult,CachePut,CacheRemoveorCacheRemoveAllare invoked.A bean of type
CacheManagermust be registered, as there is no reasonable default that the framework can use as a convention. And whereas the<cache:annotation-driven>element assumes a bean named "cacheManager",@EnableCachingsearches for a cache manager bean by type. Therefore, naming of the cache manager bean method is not significant.For those that wish to establish a more direct relationship between
@EnableCachingand the exact cache manager bean to be used, theCachingConfigurercallback interface may be implemented. Notice the@Override-annotated methods below:@Configuration @EnableCaching public class AppConfig extends CachingConfigurerSupport { @Bean public MyService myService() { // configure and return a class having @Cacheable methods return new MyService(); } @Bean @Override public CacheManager cacheManager() { // configure and return an implementation of Spring's CacheManager SPI SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default"))); return cacheManager; } @Bean @Override public KeyGenerator keyGenerator() { // configure and return an implementation of Spring's KeyGenerator SPI return new MyKeyGenerator(); } }This approach may be desirable simply because it is more explicit, or it may be necessary in order to distinguish between twoCacheManagerbeans present in the same container.Notice also the
keyGeneratormethod in the example above. This allows for customizing the strategy for cache key generation, per Spring'sKeyGeneratorSPI. Normally,@EnableCachingwill configure Spring'sSimpleKeyGeneratorfor this purpose, but when implementingCachingConfigurer, a key generator must be provided explicitly. Returnnullornew SimpleKeyGenerator()from this method if no customization is necessary.CachingConfigureroffers additional customization options: it is recommended to extend fromCachingConfigurerSupportthat provides a default implementation for all methods which can be useful if you do not need to customize everything. SeeCachingConfigurerJavadoc for further details.The
mode()attribute controls how advice is applied: If the mode isAdviceMode.PROXY(the default), then the other attributes control the behavior of the proxying. Please note that proxy mode allows for interception of calls through the proxy only; local calls within the same class cannot get intercepted that way.Note that if the mode() is set to
AdviceMode.ASPECTJ, then the value of theproxyTargetClass()attribute will be ignored. Note also that in this case thespring-aspectsmodule JAR must be present on the classpath, with compile-time weaving or load-time weaving applying the aspect to the affected classes. There is no proxy involved in such a scenario; local calls will be intercepted as well.- Since:
- 3.1
- Author:
- Chris Beams, Juergen Hoeller
- See Also:
CachingConfigurer,CachingConfigurationSelector,ProxyCachingConfiguration,AspectJCachingConfiguration
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description AdviceModemodeIndicate how caching advice should be applied.intorderIndicate the ordering of the execution of the caching advisor when multiple advices are applied at a specific joinpoint.booleanproxyTargetClassIndicate whether subclass-based (CGLIB) proxies are to be created as opposed to standard Java interface-based proxies.
Element Detail
proxyTargetClass
boolean proxyTargetClass
Indicate whether subclass-based (CGLIB) proxies are to be created as opposed to standard Java interface-based proxies. The default isfalse. Applicable only ifmode()is set toAdviceMode.PROXY.Note that setting this attribute to
truewill affect all Spring-managed beans requiring proxying, not just those marked with@Cacheable. For example, other beans marked with Spring's@Transactionalannotation will be upgraded to subclass proxying at the same time. This approach has no negative impact in practice unless one is explicitly expecting one type of proxy vs another, e.g. in tests.- Default:
- false
mode
AdviceMode mode
Indicate how caching advice should be applied.The default is
AdviceMode.PROXY. Please note that proxy mode allows for interception of calls through the proxy only. Local calls within the same class cannot get intercepted that way; a caching annotation on such a method within a local call will be ignored since Spring's interceptor does not even kick in for such a runtime scenario. For a more advanced mode of interception, consider switching this toAdviceMode.ASPECTJ.- Default:
- org.springframework.context.annotation.AdviceMode.PROXY
order
int order
Indicate the ordering of the execution of the caching advisor when multiple advices are applied at a specific joinpoint.The default is
Ordered.LOWEST_PRECEDENCE.- Default:
- 2147483647