gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:34:17 26 4
gpt4 key购买 nike

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

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

由于它在本地工作,我将其部署到服务器上,显然还有另一个带有 CacheManger 的应用程序正在竞争它的空间因为我得到以下堆栈跟踪:

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()不会返回spring CacheManger。我尝试将返回的 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,并且正如第一个异常(exception)所示,单个 JVM 中不能有多个。

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

关于java - 将 net.sf.ehcache.CacheManger 转换为 org.springframework.cache.CacheManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45469815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com