Another unnamed CacheManager already exists in the same VM
今天学习Spring 缓存机制。遇到不少问题~
好不easy缓存的单元測试用例调试成功了,在同一项目下单元測试另外一个文件时,发生了异常:
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下一跑。类似异常又发生了。经过一番周折之后,最终见着曙光了~
<?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缓存的问题全然攻克了~
Another unnamed CacheManager already exists in the same VM的更多相关文章
- hibernate3与ehcache-2.8.3配合使用,在多个SessionFactory实例的情况下出现“Another unnamed CacheManager already exists in the same VM”问题
不想看分析过程的,直接拉到最下面看第4部分 1. 问题背景 由于某些原因,项目中使用了hibernate3与ehcache-2.8.3在配合使用,又由于某些原因,需要使用多个SessionFactor ...
- 解决spring+shiro cacheManager 登录报错
一.项目启动,登录报错 org.springframework.beans.factory.BeanCreationException: Error creating bean with name ' ...
- cacheManager载入问题
net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please pr ...
- spring整合ehcache2.5.2缓存异常-- net.sf.ehcache.CacheException
报错如下: The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcach ...
- 关于Springboot 中注入多个cacheManage 时候 存在报错
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'e ...
- Shiro+SpringMVC 实现更安全的登录(加密匹配&登录失败超次数锁定帐号)
原文:http://blog.csdn.net/wlwlwlwl015/article/details/48518003 前言 初学shiro,shiro提供了一系列安全相关的解决方案,根据官方的介绍 ...
- 一个tomcat部署多个应用实例总结
项目组有好几个项目需要运行,之前项目少,一个tomcat对应一个项目还能应付,但现在项目多了,要是再一个tomcat对应一个项目的话,一方面看起来很业余,一方面也加大服务器的维护难度.所以现在需要对t ...
- ehcache hibernate4整合问题
当在原有hibernate使用ehcache缓存的项目中加入ehcache时,报如下错误时 Caused by: org.hibernate.service.spi.ServiceException: ...
- SpringMVC + Mybatis + Shiro + ehcache时缓存管理器报错。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' ...
随机推荐
- pip更新产生的问题及其解决方法?
运行 pip3 install --upgrade pip 发生错误: from pip import main ImportError: cannot import name 'main' 将以下代 ...
- BZOJ 2190:[SDOI2008]仪仗队(欧拉函数)
[SDOI2008]仪仗队 Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视 ...
- 【Luogu】P4067储能表(数位DP)
题目链接 好的 看到这题之后我一直在想反演,然后想不出来,一度以为自己脑子有问题 然后我脑子真的有问题,这题tm根本就不是反演 设f[i][j][k][l]表示现在已经DP到从高位往低数的第i位,有没 ...
- SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- [HNOI2004][bzoj1212] L语言 [Trie+dp]
题面 传送门 思路 无后效性 显然,不管某个前缀的理解方式是怎么样的,如果它能被理解,那么前面的决策对于后面的决策而言都是等价的 因此这题可以DP DP方程 令$dp[i]$表示前缀i是否能被理解 那 ...
- [canvas入坑2]模糊效果
查看效果请到 http://philippica.github.io/ 点击blur 模糊效果比较好的应该是高斯模糊,一个点的值变成了以该点为圆心的一个圆内所有像素的加权平均,权重由二维正态分布计算 ...
- 二叉树节点个数,叶子个数,第K层个数,最低公共节点
1. 节点个数 function getNodeNum(root){ if(root == null){ return 0; } //+1为root的计数 return getNodeNum(root ...
- 洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day
P2866 [USACO06NOV]糟糕的一天Bad Hair Day 75通过 153提交 题目提供者洛谷OnlineJudge 标签USACO2006云端 难度普及/提高- 时空限制1s / 12 ...
- 心跳 CSS
生活中我们所见到的大部分图形(正方形.长方形.圆形.椭圆.三角形.多边形...)都是可以用css3来实现,以及一些复杂点的图形——其实都是由基本图形组合而成的. 由于明天就是情人节了,所以今天我们就用 ...
- pat 甲级 1065. A+B and C (64bit) (20)
1065. A+B and C (64bit) (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming G ...