Another unnamed CacheManager already exists in the same VM

本文详细介绍了在Spring框架中使用ehcache进行缓存配置的过程,包括解决配置冲突、多配置文件管理以及缓存测试用例的实现。通过调整配置文件的位置和命名,避免了在同一VM中出现多个相同名称的CacheManager导致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天学习Spring 缓存机制,遇到不少问题~

好不容易缓存的单元测试用例调试成功了,在同一项目下单元测试另外一个文件时,发生了异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManagerFactory' defined in file [D:\workspaces\eclipse_svn\NewsPortalProject\WebContent\WEB-INF\classes\config\ehcache.applicationContext.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 necessary
2. Shutdown the earlier cacheManager before creating new one with same name.


最后发现原因:

由于项目下有三个配置文件,都放在同一目录下:

在另个单元测试用例中实例化Spring容器的时候,所有的配置文件都加载进去了

ApplicationContext aCtx = new FileSystemXmlApplicationContext("classpath:config/*.xml");

解决办法:将缓存的配置文件和其他的配置文件放在不同包下

1.缓存测试用例中,实例化容器时,只读缓存相关的配置文件;

ApplicationContext aCtx = new FileSystemXmlApplicationContext("classpath:ehcache/*.xml");
2.其他用例也只读自己的配置文件;

ApplicationContext aCtx = new FileSystemXmlApplicationContext("classpath:config/*.xml");


最后贴一段实现缓存的单元测试用例,机器配置文件

配置文件路径:/project/src/test/resources/ehcache

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 updateCheck="false">
    <diskStore path="java.io.tmpdir"/> 
    <defaultCache 
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="10"
           timeToLiveSeconds="10"
           overflowToDisk="false"/>
    <cache name="cachelist"
    	   eternal="false"
    	   timeToIdleSeconds="360" 
           timeToLiveSeconds="3600"
           maxElementsInMemory="100"
           overflowToDisk="false"
           diskPersistent="false"
           memoryStoreEvictionPolicy="LRU" />
</ehcache>

ehcache.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:oxm="http://www.springframework.org/schema/oxm"  
    xmlns:mvc="http://www.springframework.org/schema/mvc"  
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd"> 
    <!--  缓存  属性-->
    <context:component-scan base-package="com.test" />
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
        <property name="configLocation"  value="classpath:ehcache/ehcache.xml"/> 
	</bean> 
	<!-- 支持缓存注解 -->
    <cache:annotation-driven cache-manager="cacheManager" />
    <!-- 默认是cacheManager -->
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >  
        <property name="cacheManager"  ref="cacheManagerFactory"/>
    </bean>  
</beans> 


DirectcityCacheTest.java

package com.test.news.util;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;


/**   
 *    
 * 项目名称:NewsPortalProject   
 * 类名称:DirectcityCacheTest   
 * 类描述:   
 * 创建人:XianJuanJuan   
 * 创建时间:2015年6月2日 下午2:11:45    
 * @version    
 *    
 */
public class DirectcityCacheTest {
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@SuppressWarnings("resource")
	@Test
	public void init() {
		ApplicationContext aCtx = new FileSystemXmlApplicationContext("classpath:ehcache/*.xml");
		CacheManager cacheManager = (CacheManager) aCtx.getBean("cacheManager");
		// 为了测试缓存是否成功,这里加一个
		for (int i = 0; i<5;i++) {
			Object obj = cacheManager.getCache("newslist").get("a1");
			String data = null;
			if (obj == null) {
				System.out.println("i="+i+";第一次执行该方法,缓存中还没有数据");
			} else {
				data = (String) cacheManager.getCache("newslist").get("a1").get();
				System.out.println("i="+i+";第"+i+"次读到的缓存中的内容:"+cacheManager.getCache("newslist").get("a1").get());
			}
			String list = ""+i;
			if (data == null) {
				list = list + "String";
				if(list != null && !"".equals(list)) {
					Cache cache = cacheManager.getCache("newslist");
					if (cache != null) {
						cache.put("a1", list);
					}
				}

			}
		}
	}
}

结果:


单元测试调试好的代码,放在resin下一跑,类似异常又发生了,经过一番周折之后,终于见着曙光了~
具体解决办法:
1.将项目中其他文件夹下多余的配置文件删掉,最终
src/main/resources/ehcache/ehcache.xml
WebContent/WEB-INF/config/ehcache.applicationContext.xml  
WebContent/WEB-INF/config/springmvc-servlet.xml
2.将D:\resin-4.0.35\webapps\ProjectTest\WEB-INF和D:\resin-4.0.35\webapps\ProjectTest\WEB-INF\classes关于配置文件的文件夹都删掉~
3.重新编译+clean项目之后,最终生成的配置文件位置
D:\resin-4.0.35\webapps\ProjectTest\WEB-INF\classes\ehcache\ehcache.xml
D:\resin-4.0.35\webapps\ProjectTest\WEB-INF\config\ehcache.applicationContext.xml  
D:\resin-4.0.35\webapps\ProjectTest\WEB-INF\config\springmvc-servlet.xml

注:配置文件多次提到的类路径在这里表示:classpath=D:\resin-4.0.35\webapps\ProjectTest\WEB-INF\classes

4.需要给配置的ehcache加个name属性,来标注他的唯一性
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
 updateCheck="false" >
    <diskStore path="java.io.tmpdir"/> 
    <defaultCache 
           maxElementsInMemory="1000"
           eternal="false"
           timeToIdleSeconds="10"
           timeToLiveSeconds="10"
           overflowToDisk="false"/>
    <cache name="cachelist"
    	   eternal="false"
    	   timeToIdleSeconds="360" 
           timeToLiveSeconds="3600"
           maxElementsInMemory="100"
           overflowToDisk="false"
           diskPersistent="false"
           memoryStoreEvictionPolicy="LRU" />
</ehcache>

在解决问题时论坛中看到这么一段,貌似有朋友的问题就得到了解决,但对我没起作用~先贴下来,有时间了再学习学习

我靠,终于解决了,原来是ehcache版本的问题,ehcache-core2.5.0之前的版本不会出问题,2.5.0及之后会报这个异常,我选了个ehcache-core2.4.8的好使。

<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
 <version>2.4.8</version>
</dependency>

看官方的文档说明:

Versions of Ehcache before version 2.5 allowed any number of CacheManagers with the same name (same configuration resource) to exist in a JVM.

Ehcache 2.5 and higher does not allow multiple CacheManagers with the same name to exist in the same JVM. CacheManager() constructors creating non-Singleton CacheManagers can violate this rule



至此,Spring 中的ehcache缓存的问题完全解决了~



转载于:https://www.cnblogs.com/jeofey/p/7227070.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值