Spring boot+shiro報Another CacheManager with same name 'shiroCache' already exists in the same VM

在Spring Boot項目中整合Shiro和Ehcache緩存時發生如下的異常信息:

Caused by: net.sf.ehcache.CacheException: Another CacheManager with same name 'shiroCache' 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@16a49a5d]

在網絡搜索了一番後通過以下方式解決該異常:

在ShiroConfig中添加以下三項配置

   @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
        EhCacheManagerFactoryBean factoryBean = new EhCacheManagerFactoryBean();
        factoryBean.setShared(true);
        return factoryBean;
    }

    /**
     * LifecycleBeanPostProcessor將統一管理Initializable和Destroyable的實現類,從而達到統一管理Shiro Bean的生命週期的目的
     */
    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }

    /**
     * DefaultAdvisorAutoProxyCreator的作用是動態代理Shiro的事務,最終將事務交由Spring進行統一管理。此配置項需要依賴於LifecycleBeanPostProcessor
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return  defaultAdvisorAutoProxyCreator;
    }

通過添加上述的三個配置項,在Spring Boot啓動時,還是出相同的異常,異常如下:

Caused by: org.apache.shiro.cache.CacheException: net.sf.ehcache.CacheException: Another CacheManager with same name 'shiroCache' 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: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]

思考後猜想可能是在項目中shiro加入緩存管理器,代碼如下:

/**
     * 會話管理器
     * @return sessionManager
     */
    @Bean
    public DefaultWebSessionManager configWebSessionManager() {
        DefaultWebSessionManager manager = new DefaultWebSessionManager();
        // 加入緩存管理器
        manager.setCacheManager(EhCacheUtil.getInstance());
        // 刪除過期的session
        manager.setDeleteInvalidSessions(false);
        // 設置全局session超時時間 單位ms  當前設置爲30min
        // manager.setGlobalSessionTimeout(30 * 60 * 1000);

        // 設置監聽器
        ArrayList<SessionListener> lisener = new ArrayList<>();
        lisener.add(new ShiroSessionListener());
        manager.setSessionListeners(lisener);
        // 是否定時檢查session
        manager.setSessionValidationSchedulerEnabled(true);
        manager.setSessionValidationScheduler(configSessionValidationScheduler());
        manager.setSessionIdCookieEnabled(true);

        return manager;
    }
/**
 * @author He Changjie
 */
public class EhCacheUtil {
    private static EhCacheManager ehCacheManager = new EhCacheManager();
    private static boolean init = false;

    public EhCacheUtil() {}

    public static EhCacheManager getInstance(){
        if(!init){
            ehCacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
            init = true;
        }
        return ehCacheManager;
    }
}

同時又使用了redis所造成的該異常,於是進行了以下操作:

將ehcache.xml重命名爲ehcache-shrio.xml,同時將ehcache加載xml的名稱同步更改,

ehCacheManager.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");

該異常被神奇的解決了!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章