小编Eri*_*ich的帖子

在没有XML的情况下在Spring 4中使用EhCache

有没有办法在Spring 4或Spring Boot中初始化没有xml的EhCache?

我注意到Spring Boot 1.0.0.RC3没有任何ehcache依赖,但Spring 4.0GA发布文章提到它改进了对EhCache的支持.此外,Spring 3有类,org.springframework.cache.ehcache.EhCacheCacheManager但它不再是依赖项的一部分.

编辑: Spring 4确实有EhCache支持.您必须添加依赖项:

<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
Run Code Online (Sandbox Code Playgroud)

编辑2: 我尝试了以下内容,我认为我很接近但是我收到了一个错误:

@Bean
@Override
public CacheManager cacheManager() {
    CacheConfiguration cacheConfiguration = new CacheConfiguration();
    cacheConfiguration.setName("primary");
    cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
    cacheConfiguration.setMaxEntriesLocalHeap(0);

    net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
    config.addCache(cacheConfiguration);

    net.sf.ehcache.CacheManager cacheManager = new net.sf.ehcache.CacheManager(config);
    cacheManager.setName("EhCache");

    return new EhCacheCacheManager(cacheManager);
}

@Bean
public EhCacheManagerFactoryBean factoryBean() {
    return new EhCacheManagerFactoryBean();
}
Run Code Online (Sandbox Code Playgroud)

错误

Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the …
Run Code Online (Sandbox Code Playgroud)

java spring ehcache

34
推荐指数
2
解决办法
5万
查看次数

标签 统计

ehcache ×1

java ×1

spring ×1