spring 整合 ehcache失败记录

来源:互联网 发布:威少刷数据 编辑:程序博客网 时间:2024/05/29 13:36

按照网上教程进行 spring 与 ehcache 整合,整合后启动项目发现报错,报错信息如下:

[ERROR][2017-08-07 11:01:07] org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:350) Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in URL [jar:file:/E:/apache-tomcat-7.0.68/webapps/renren-web/WEB-INF/lib/renren-common-2.1.0.jar!/renren-cache.xml]: Cannot resolve reference to bean 'ehcache' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehcache' defined in URL [jar:file:/E:/apache-tomcat-7.0.68/webapps/renren-web/WEB-INF/lib/renren-common-2.1.0.jar!/renren-cache.xml]: Invocation of init method failed; nested exception is 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 necessary2. Shutdown the earlier cacheManager before creating new one with same name.The source of the existing CacheManager is: InputStreamConfigurationSource [stream=sun.net.www.protocol.jar.JarURLConnection$JarURLInputStream@1a1d48f2]at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1259)at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1998)at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)at java.util.concurrent.FutureTask.run(FutureTask.java:262)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)at java.lang.Thread.run(Thread.java:745)
什么意思呢?经查询后发现是因为 

ehcache在2.5以后,CacheManager使用了Singleton,这样在创建多个CacheManager时就会产生上述错误。

http://ehcache.org/documentation/get-started/concepts 是官方解释

我使用是是2.10 版本的

并且项目中还引用了shiro框架,shiro 使用ehcache做缓存,这样就造成多个cacheManager的出现。

解决办法:

在 创建bean的时候加入 <property name="shared" value="true" /> <!-- 指定缓存管理器共享共享-->

 
 <!-- 启用缓存注解开关 --><cache:annotation-driven cache-manager="cacheManager"/>    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">            <property name="cacheManager" ref="ehcache"></property>        </bean>    <!-- EhCache library setup --><bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml"/><property name="shared" value="true" />

这样就解决了多cacheManager的问题



原创粉丝点击