Another unnamed CacheManager already exists in the same VM (ehCache 2.5) 解决办法

来源:互联网 发布:区块链卿毅 知乎 编辑:程序博客网 时间:2024/05/24 04:51

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即可。

阅读全文
0 0
原创粉丝点击