<cache:annotation-driven />的非XML版本

sto*_*vik 4 configuration spring caching

要使基于注释的缓存魔法与Spring一起工作,需要在xml中有一个声明,如下所示: <cache:annotation-driven />

如何以编程方式配置缓存系统?

这就是我所拥有的.我想摆脱@ImportResource和XML文件.

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

ska*_*man 8

Spring 3.1 RC2添加了@EnableCaching注释,RC1中没有.此注释相当于<cache:annotation-driven />并添加到您的@Configuration类中:

@Configuration
@EnableCaching
public class DI_EhCache {
Run Code Online (Sandbox Code Playgroud)

RC2似乎并没有被公布,并且该文档不从网站链接的,但你可以下载它在这里,看文档的@EnableCaching 位置.