To get the annotation-based caching-magic to work with Spring, one need to have a declaration in xml, like this:
<cache:annotation-driven />
How can I get the caching system configured programmatically?
This is what I have. I want to get rid of the @ImportResource
and the XML file.
@Configuration
@ImportResource("classpath:cache-context.xml")
public class DI_EhCache {
/**
* Create cache object for various cachable methods and add to EhCache Manager.
*
* @return EhCacheManager
*/
@Bean
EhCacheCacheManager cacheManager() {
EhCacheCacheManager ehcm = new EhCacheCacheManager();
CacheManager cm = CacheManager.create();
Cache station = new Cache("station", 1, false, true, 0, 10);
cm.addCache(station);
ehcm.setCacheManager(cm);
return ehcm;
}
}
Spring 3.1 RC2 added the @EnableCaching
annotation, which wasn't present in RC1. This annotation is the equivalent of <cache:annotation-driven />
, and is added to your @Configuration
class:
@Configuration
@EnableCaching
public class DI_EhCache {
Rc2 doesn't seem to have been announced, and the docs are not linked to from the site, but you can download it here, and see the docs for @EnableCaching
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With