springboot+shiro+ehcache整合之Another unnamed CacheManager already exists in the same VM.

ehcache.xml配置文件


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false"
         name="myEhcache">
    <diskStore path="java.io.tmpdir/Tmp_EhCache"/>
    <defaultCache eternal="false"
                  maxElementsInMemory="1000"
                  overflowToDisk="false"
                  diskPersistent="false"
                  timeToIdleSeconds="0"
                  timeToLiveSeconds="600"
                  memoryStoreEvictionPolicy="LRU"/>
    <cache name="user"
           eternal="false"
           maxElementsInMemory="10000"
           overflowToDisk="false"
           diskPersistent="false"
           timeToIdleSeconds="0"
           timeToLiveSeconds="0"
           memoryStoreEvictionPolicy="LFU"/>
    
ehcache>

注意:
1、原因是:如果使用的ehcache版本超过了2.5.0,那么就很重要,不然ehcache会自动加载为默认的名字_default_,而ehcache2.5以后只允许创建单例的CacheManager,从而报重复加载CacheManager的错误。

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManager' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/EhCacheCacheConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [net.sf.ehcache.CacheManager]: Factory method 'ehCacheCacheManager' threw exception; 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.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@5f61d894]

2、解决方法
第一种:jar降级

<dependency>
<groupId>net.sf.ehcachegroupId>
<artifactId>ehcache-coreartifactId>
 <version>2.4.8version>
dependency> 
不过,如果是采用springboot的pom.xml统一版本号管理就有可能改了ehcache还是没有什么作用,这时可以采用第二种

第二种:创建ehCacheManager的时候,先判断是否有cacheManager,没有的情况下再进行创建,判断的关键就是我们一开始在编写ehcache.xml的时候强调的name属性,通过该属性来判断cacheManager是否加载了。

@Bean
    public EhCacheManager ehCacheManager() {
        //注意myEhcache对应上面的
        CacheManager cacheManager = CacheManager.getCacheManager("myEhcache");
        if(cacheManager == null){
            cacheManager = CacheManager.create();
        }
        EhCacheManager ehCacheManager = new EhCacheManager();
        ehCacheManager.setCacheManager(cacheManager);
        return ehCacheManager;
    }

你可能感兴趣的:(缓存,SpringBoot,springboot学习之路)