Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-XML version of <cache:annotation-driven />

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;
    }
}
like image 293
stolsvik Avatar asked Feb 22 '23 04:02

stolsvik


1 Answers

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.

like image 99
skaffman Avatar answered Feb 26 '23 22:02

skaffman