Another unnamed CacheManager already exists in the same VM

Source: Internet
Author: User
Tags aop

Today learning spring caching mechanism, encountered a lot of problems ~

The cached unit test case was debugged successfully, and an exception occurred while the unit was testing another file under the same project:

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-CacheManager in the config or do one of the following:1. Use one of the cachemanager.create () static factory methods to reuse same CacheManager with same name or create one if NEC Essary2. Shutdown the earlier CacheManager before creating new one with same name.


Finally found the reason:

Because there are three profiles under the project, they are placed in the same directory:

When you instantiate a spring container in another unit test case, all the configuration files are loaded in

ApplicationContext aCtx = new Filesystemxmlapplicationcontext ("Classpath:config/*.xml");

Workaround: Place the cached configuration file and other configuration files under different packages

1. Cache test cases, when instantiating a container, read-only cache-related configuration files;

ApplicationContext aCtx = new Filesystemxmlapplicationcontext ("Classpath:ehcache/*.xml");
2. Other use cases also read only their own configuration files;

ApplicationContext aCtx = new Filesystemxmlapplicationcontext ("Classpath:config/*.xml");


Finally, a unit test case for implementing the cache, machine configuration file

Configuration file path:/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=" "           eternal=" false "           Timetoidleseconds= "Ten"           timetoliveseconds= "overflowtodisk="           false "/>    <cache name=" Newslist "       eternal= "false"       timetoidleseconds= "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" XM Lns: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/bean S/spring-beans-3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schem A/context/spring-context-3.2.xsd Http://www.springframework.org/schema/cache http://www.springframework.org  /schema/cache/spring-cache.xsd "> <!--Cache Properties--<context:component-scan base-package= "com.test"/> <bean id= "cachemanagerfactory" class= "Org.s Pringframework.cache.ehcache.EhCacheManagerFactoryBean "> <property name=" configlocation "value=" classpath: Ehcache/ehcache.xml "/> </bean> <!--support cache annotations--<cache:annotation-driven cache-manager=" CacheManager "/> <!--default is CacheManager--<bean id=" CacheManager "class=" ORG.SPRINGFRAMEWORK.CACHE.EHCAC He. 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;/** * * Project Name: Newsportalproject * Class Name: DIRECTC Itycachetest * Class Description: * Creator: Xianjuanjuan * creation time: June 2, 2015 PM 2:11:45 * @version * */public class DIRECTC itycachetest {@BeforeClasspublic static void Setupbeforeclass () throws Exception {} @SuppressWarnings ("resource") @ testpublic void init () {ApplicationContext aCtx = new Filesystemxmlapplicationcontext ("Classpath:ehcache/*.xml"); CacheManager CacheManager = (CacheManager) actx.getbean ("CacheManager");//To test whether the cache is successful, add a for (int i = 0; i<5;i++) {O Bject obj = Cachemanager.getcache ("Newslist"). Get ("A1"); String data = null;if (obj = = null) {System.out.println ("i=" +i+ "; the method was first executed, there is no data in the cache");} else {datasource = (String) CacheManager. GetcachE ("Newslist"). Get ("A1"). Get (); System.out.println ("i=", "+i+"; "+i+" reads the contents of the cache: "+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);}}}}}

Results:


Another unnamed CacheManager already exists in the same VM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.