将 net.sf.ehcache.CacheManger 转换为 org.springframework.cache.CacheManager?
Convert net.sf.ehcache.CacheManger to org.springframework.cache.CacheManager?
我有一个应用程序在本地运行,在 Application.java
中有一个用于 Spring Boot 的 bean,名为 cacheManager
@Bean(name="cacheManager")
@Primary
public CacheManager getCacheManager() {
return new EhCacheCacheManager();
}
因为它在本地工作,所以我部署到服务器,显然还有另一个应用程序正在竞争它的 CacheManger space
因为我得到以下堆栈跟踪:
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:
DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] at
net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626)
at net.sf.ehcache.CacheManager.init(CacheManager.java:391) at
net.sf.ehcache.CacheManager.(CacheManager.java:269) at
org.springframework.cache.ehcache.EhCacheManagerUtils.buildCacheManager(EhCacheManagerUtils.java:54)
at
org.springframework.cache.ehcache.EhCacheCacheManager.afterPropertiesSet(EhCacheCacheManager.java:74)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
at
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
... 32 common frames omitted
我试图把
@Bean(name="cacheManager")
@Primary
public CacheManager getCacheManager() {
return net.sf.ehcache.CacheManager.create();
}
但是 net.sf.ehcache.CacheManger.create()
没有 return spring CacheManger。我尝试将 returning CacheManager 更改为 net.sf.ehcache.CacheManager,但我在本地得到了这个:
Caused by: java.lang.IllegalStateException: No CacheResolver
specified, and no unique bean of type CacheManager found. Mark one as
primary (or give it the name 'cacheManager') or declare a specific
CacheManager to use, that serves as the default one. at
org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:212)
at
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781)
at
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)
at
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)
at
org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at
org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)
at
org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)
at
org.springframework.boot.SpringApplication.run(SpringApplication.java:314)
at
org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151)
at
org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131)
at
org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86)
at
org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
at
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156)
at
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 42 more
我认为转换是答案,但答案也可能是一些狡猾的代码移动。
建议?
额外信息:这是在网络服务中
除非您为 Ehcache 部署 ehcache.xml
配置文件,否则您将获得默认的嵌入式配置。此配置未命名 CacheManager
并且如第一个异常所示,您不能在单个 JVM 中拥有多个。
最简单的解决方案是有一个 ehcache.xml
,而不是在一个包中,然后它会被您的部署拾取。
我的问题的答案是让 Spring 决定缓存管理器,所以我需要做的就是在我的 Application.java 上添加 @EnableCaching,然后在我想要的方法上使用 @Cacheable在服务器上缓存。
我有一个应用程序在本地运行,在 Application.java
中有一个用于 Spring Boot 的 bean,名为 cacheManager
@Bean(name="cacheManager")
@Primary
public CacheManager getCacheManager() {
return new EhCacheCacheManager();
}
因为它在本地工作,所以我部署到服务器,显然还有另一个应用程序正在竞争它的 CacheManger space 因为我得到以下堆栈跟踪:
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: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:626) at net.sf.ehcache.CacheManager.init(CacheManager.java:391) at net.sf.ehcache.CacheManager.(CacheManager.java:269) at org.springframework.cache.ehcache.EhCacheManagerUtils.buildCacheManager(EhCacheManagerUtils.java:54) at org.springframework.cache.ehcache.EhCacheCacheManager.afterPropertiesSet(EhCacheCacheManager.java:74) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ... 32 common frames omitted
我试图把
@Bean(name="cacheManager")
@Primary
public CacheManager getCacheManager() {
return net.sf.ehcache.CacheManager.create();
}
但是 net.sf.ehcache.CacheManger.create()
没有 return spring CacheManger。我尝试将 returning CacheManager 更改为 net.sf.ehcache.CacheManager,但我在本地得到了这个:
Caused by: java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary (or give it the name 'cacheManager') or declare a specific CacheManager to use, that serves as the default one. at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:212) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:781) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:151) at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:131) at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:86) at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5156) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) ... 42 more
我认为转换是答案,但答案也可能是一些狡猾的代码移动。 建议? 额外信息:这是在网络服务中
除非您为 Ehcache 部署 ehcache.xml
配置文件,否则您将获得默认的嵌入式配置。此配置未命名 CacheManager
并且如第一个异常所示,您不能在单个 JVM 中拥有多个。
最简单的解决方案是有一个 ehcache.xml
,而不是在一个包中,然后它会被您的部署拾取。
我的问题的答案是让 Spring 决定缓存管理器,所以我需要做的就是在我的 Application.java 上添加 @EnableCaching,然后在我想要的方法上使用 @Cacheable在服务器上缓存。