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

时间:2011-12-07 15:45:21

标签: spring caching configuration

要使用基于注释的缓存魔法与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;
    }
}

1 个答案:

答案 0 :(得分:8)

Spring 3.1 RC2添加了@EnableCaching注释,这在RC1中不存在。此注释相当于<cache:annotation-driven />,并已添加到您的@Configuration类:

@Configuration
@EnableCaching
public class DI_EhCache {

Rc2似乎尚未公布,并且文档未与网站相关联,但您可以下载here,并查看@EnableCaching here的文档