Есть ли способ инициализировать EhCache без xml в Spring 4 или Spring Spring?Использование EhCache весной 4 без XML
Я заметил Spring Boot 1.0.0.RC3 не имеет никаких зависимостей ehcache, но Spring 4.0GA release post упомянул, что у него улучшенная поддержка EhCache. Кроме того, Spring 3 имел класс org.springframework.cache.ehcache.EhCacheCacheManager
, но это уже не часть зависимостей.
Редактировать: Весна 4 имеет поддержку EhCache. Вы должны добавить зависимость:
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
Edit2: Я попытался следующие, и я думаю, что я близко, но я получаю сообщение об ошибке:
@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'. См. Https://github.com/spring-projects/spring-framework/tree/master/spring-context-support/src/main/java/org/springframework/cache/ehcache. –