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

标签 java spring spring-boot ehcache

我有一个应用程序,使用 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/

相关文章:

java - 如何在java中迭代开始日期到结束日期

java - 网络应用程序 : safetly update a shared List/Map in the AppContext

java - java 中的同步给出了意外的输出

Java 8 DateTimeFormatter 忽略毫秒和区域

spring - Spring MVC 中应用程序级别的 Bean 应该放在哪里?

java - 引用 spring bean 中的一组属性

java - 我执行了dao层的mybatis update方法,但是数据库没有更新

spring - 使用 Spring Boot 外部 log4.properties 文件

java - 在非嵌入式 Tomcat 上使用 SSL 运行 SpringBoot

java - spring请求体将名称分配给数组/列表