【发布时间】:2014-03-23 13:03:58
【问题描述】:
有没有办法在 Spring 4 或 Spring Boot 中不使用 xml 来初始化 EhCache?
我注意到 Spring Boot 1.0.0.RC3 没有任何 ehcache 依赖项,但 Spring 4.0GA release post 提到它改进了对 EhCache 的支持。此外,Spring 3 有 org.springframework.cache.ehcache.EhCacheCacheManager 类,但这不再是依赖项的一部分。
编辑: Spring 4 确实支持 EhCache。您必须添加依赖项:
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
编辑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();
}
错误
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: [Programmatically configured]
at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:590)
at net.sf.ehcache.CacheManager.init(CacheManager.java:384)
at net.sf.ehcache.CacheManager.<init>(CacheManager.java:263)
at org.springframework.cache.ehcache.EhCacheManagerFactoryBean.afterPropertiesSet(EhCacheManagerFactoryBean.java:166)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 15 more
【问题讨论】:
-
Spring4 仍然有那个类,所以不确定你在哪里知道它不再工作了。它在
spring-context-support罐子里。见github.com/spring-projects/spring-framework/tree/master/…。