在没有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>

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

回答:

Spring中EhCache的无XML配置

@Configuration

@EnableCaching

public class CachingConfig implements CachingConfigurer {

@Bean(destroyMethod="shutdown")

public net.sf.ehcache.CacheManager ehCacheManager() {

CacheConfiguration cacheConfiguration = new CacheConfiguration();

cacheConfiguration.setName("myCacheName");

cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");

cacheConfiguration.setMaxEntriesLocalHeap(1000);

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

config.addCache(cacheConfiguration);

return net.sf.ehcache.CacheManager.newInstance(config);

}

@Bean

@Override

public CacheManager cacheManager() {

return new EhCacheCacheManager(ehCacheManager());

}

@Bean

@Override

public KeyGenerator keyGenerator() {

return new SimpleKeyGenerator();

}

@Bean

@Override

public CacheResolver cacheResolver() {

return new SimpleCacheResolver();

}

@Bean

@Override

public CacheErrorHandler errorHandler() {

return new SimpleCacheErrorHandler();

}

}

另外,为了进行测试,你可以使用一个简单的ConcurrentMapCache来运行,而无需XML,如下所示。

@Configuration

@EnableCaching

public class CachingConfig implements CachingConfigurer {

@Bean

@Override

public CacheManager cacheManager() {

SimpleCacheManager cacheManager = new SimpleCacheManager();

List<Cache> caches = new ArrayList<Cache>();

caches.add(new ConcurrentMapCache("myCacheName"));

cacheManager.setCaches(caches);

return cacheManager;

}

@Bean

@Override

public KeyGenerator keyGenerator() {

return new SimpleKeyGenerator();

}

@Bean

@Override

public CacheResolver cacheResolver() {

return new SimpleCacheResolver();

}

@Bean

@Override

public CacheErrorHandler errorHandler() {

return new SimpleCacheErrorHandler();

}

}

编辑:更新以在基础缓存上添加关闭方法

编辑:添加了错误处理程序和缓存解析器的配置

编辑:更改SimpleCacheResolver解决CacheManager must not be null问题的构造函数调用(Spring v5.1 +)

@Configuration

@EnableCaching

public class CachingConfig implements CachingConfigurer {

public static final String USER_CACHE_INSTANCE = "my-spring-ehcache";

private final CacheManager cacheManager;

private final net.sf.ehcache.CacheManager ehCacheManager;

public CachingConfig() {

CacheConfiguration cacheConfiguration = new CacheConfiguration();

cacheConfiguration.setName(USER_CACHE_INSTANCE);

cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");

cacheConfiguration.setMaxEntriesLocalHeap(1000);

net.sf.ehcache.config.Configuration config

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

config.addCache(cacheConfiguration);

this.ehCacheManager = net.sf.ehcache.CacheManager.newInstance(config);

this.cacheManager = new EhCacheCacheManager(ehCacheManager);

}

@Bean(destroyMethod="shutdown")

public net.sf.ehcache.CacheManager ehCacheManager() {

return ehCacheManager;

}

@Bean

@Override

public CacheManager cacheManager() {

return cacheManager;

}

@Bean

@Override

public KeyGenerator keyGenerator() {

return new SimpleKeyGenerator();

}

@Bean

@Override

public CacheResolver cacheResolver() {

return new SimpleCacheResolver(cacheManager);

}

@Bean

@Override

public CacheErrorHandler errorHandler() {

return new SimpleCacheErrorHandler();

}

}

以上是 在没有XML的Spring 4中使用EhCache 的全部内容, 来源链接: utcz.com/qa/427884.html

回到顶部