当前位置 博文首页 > 程序员石磊:Another unnamed CacheManager already exists in t
vm中存在2个CacheManager导致报错,百度了一下,大部分都是建议换成2.4的版本来解决问题,因为2.5以后不允许vm中存在2个实例。但是我就是非要用2.5那,该怎么办? 因为我要在自己的业务逻辑中操作CacheManager,我的配置文件代码如下:
<cache:annotation-driven/>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache" />
<bean id="ehcache"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:config-location="classpath:ehcache.xml" />
<mapper namespace="com.raymon.cloudq.dao.WindowCurrentStateMapper" >
<cache type="org.mybatis.caches.ehcache.EhcacheCache"
eviction="FIFO"
flushInterval="60000"
size="512"
readOnly="true"/>
mybatis配置缓存,查看源码后,发现会重新创建一个CacheManager。如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.mybatis.caches.ehcache;
import java.util.concurrent.locks.ReadWriteLock;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.apache.ibatis.cache.Cache;
public abstract class AbstractEhcacheCache implements Cache {
protected static CacheManager CACHE_MANAGER = CacheManager.create();
终于在stackoverflow找到答案,只需要配置
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
p:shared="true"/>
EhCacheManagerFactoryBean p:shared设置为true即可。
cs