首页 文章

将net.sf.ehcache.CacheManger转换为org.springframework.cache.CacheManager?

提问于
浏览
1

我有一个应用程序,在 Application.java 中为bean本地运行,用于Spring Boot,名为cacheManager

@Bean(name="cacheManager")
 @Primary
 public CacheManager getCacheManager() {
     return new EhCacheCacheManager();
}

由于它在本地工作,我部署到服务器,显然有另一个应用程序与CacheManger竞争它的空间因为我得到以下stacktrace:

引起:net.sf.ehcache.CacheException:另一个未命名的CacheManager已经存在于同一个VM中 . 请为配置中的每个CacheManager提供唯一的名称,或执行以下操作之一:1 . 使用其中一个CacheManager.create()静态工厂方法重用相同名称的相同CacheManager或在必要时创建一个2.在创建之前关闭先前的cacheManager新的同名 . 现有CacheManager的源是:net.sf.ehcache.CacheManager.init(CacheManager.java:666)net.sf.ehcache.CacheManager.init(CacheManager)中的net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626)中的DefaultConfigurationSource [ehcache.xml或ehcache-failsafe.xml] . java:391)at net.sf.ehcache.CacheManager . (CacheManager.java:269)org.springframework.cache.ehcache.EhCacheManagerUtils.buildCacheManager(EhCacheManagerUtils.java:54)at org.springframework.cache.ehcache.EhCacheCacheManager . 在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)中的afterPropertiesSet(EhCacheCacheManager.java:74) ...省略了32个常用帧

我试图放

@Bean(name="cacheManager")
@Primary
public CacheManager getCacheManager() {
    return net.sf.ehcache.CacheManager.create();
}

但是 net.sf.ehcache.CacheManger.create() 并没有返回一个Spring CacheManger . 我尝试将返回的CacheManager更改为net.sf.ehcache.CacheManager,但我在本地获取:

引起:java.lang.IllegalStateException:未指定CacheResolver,并且找不到CacheManager类型的唯一bean . 将一个标记为主要(或将其命名为“cacheManager”)或声明要使用的特定CacheManager,作为默认值 . org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:212)atg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781)at org.springframework.context.support.AbstractApplicationContext .finishBeanFactoryInitialization(AbstractApplicationContext.java:866)位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)的org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)org.springframework.boot.SpringApplication.run(SpringApplication.java:314)在org.springframew的org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151) Ork.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131)org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86)org.springframework.web.SpringServletContainerInitializer.onStartup( SpringServletContainerInitializer.java:169)org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)... 42更多

我认为转换是答案,但答案也可能是一些狡猾的代码移动 . 建议?额外信息:这是在网络服务中

2 回答

  • 1

    除非为Ehcache部署 ehcache.xml 配置文件,否则将获得默认的嵌入式配置 . 此配置未命名 CacheManager ,并且正如第一个异常所示,在单个JVM中不能有多个 .

    最简单的解决方案是拥有一个 ehcache.xml ,而不是一个包,然后它将被您的部署选中 .

  • 0

    我的问题的答案是让Spring决定缓存管理器,所以我需要做的就是在我的Application.java上添加@EnableCaching,然后在我想要在服务器上缓存的方法上使用@Cacheable .

相关问题