Another unnamed CacheManager already exists in the same VM
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Another unnamed CacheManager already exists in the same VM,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4816字,纯文字阅读大概需要7分钟。
内容图文
![Another unnamed CacheManager already exists in the same VM](/upload/InfoBanner/zyjiaocheng/1055/53903cfac58a482cab7f09a7c34e8baf.jpg)
今天学习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="newslist" 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>
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); } } } } } }
结果:
![技术分享](/upload/getfiles/default/2022/11/14/20221114024820672.jpg)
原文:http://blog.csdn.net/jeofey/article/details/46332623
内容总结
以上是互联网集市为您收集整理的Another unnamed CacheManager already exists in the same VM全部内容,希望文章能够帮你解决Another unnamed CacheManager already exists in the same VM所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。