zoukankan      html  css  js  c++  java
  • EhCache WebCache 与 SpringMVC集成时 CacheManager冲突的问题

    EhCache WebCache 与 SpringMVC集成时 CacheManager冲突的问题

    在使用EhCache Web组件时 需要将 SimplePageCachingFilter 需要用到的CacheManager。

    在EhCache2.5版本以上 同一个JVM当中不允许出现俩个相同名字的CacheManager 否则会报错。

    默认情况下SimplePageCachingFilter 与 Spring各种都会创建一个CacheManager 这样在服务器启动时就会报错。

    后来通过分析源代码解决了此问题。

    在SimplePageCachingFilter当中我们看它是如何获取CacheManager的

        protected CacheManager getCacheManager() {
            return CacheManager.getInstance();
        }

    我们通过覆写这个方法 让它读取我们的 ehcache.xml

        @Override
        protected CacheManager getCacheManager() {
            URL url = getClass().getResource("/ehcache.xml");
            return CacheManager.create(url);
        }

    上面就完成了对CacheManager的管理 接下来就还需要配置Spring的CacheManager 让他俩都读取同一个文件就可以了。

        <!-- 配置Ehcache缓存管理器,读取配置文件 -->
        <bean id="ehCacheManagerFactoryBean" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
            <property name="configLocation" value="classpath:ehcache.xml"></property>
            <property name="shared" value="true"></property>
        </bean>
        
        <!-- 配置缓存管理器,获取cache -->
        <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" >
            <property name="cacheManager" ref="ehCacheManagerFactoryBean" />
        </bean>
    shared:这个属性需要设置为true 
    因为默认情况Spring调用的是 new CacheManager()这个方法 因为上面SimplePageCachingFilter里已经创建了一个实例 所以这里在New就会抛出异常.

        public void afterPropertiesSet() throws IOException, CacheException {
            logger.info("Initializing EHCache CacheManager");
            if (this.configLocation != null) {
                InputStream is = this.configLocation.getInputStream();
                try {
                    this.cacheManager = (this.shared ? CacheManager.create(is) : new CacheManager(is));
                }
                finally {
                    is.close();
                }
            }
            else {
                this.cacheManager = (this.shared ? CacheManager.create() : new CacheManager());
            }
            if (this.cacheManagerName != null) {
                this.cacheManager.setName(this.cacheManagerName);
            }
        }


  • 相关阅读:
    弹出框背景色透明滚动条滚动仍然居中
    日常css和js小知识点记录
    手机端上传未知图片大小,js设置宽高比例
    IE6兼容透明背景图
    css考核点整理(七)-css sprites技术的使用心得
    css考核点整理(六)-水平居中定位的几种方式
    css考核点整理(五)-css3新增的常用属性
    css考核点整理(四)-css盒模型
    css考核点整理(三)-css选择器的使用
    css考核点整理(二)-css层叠机制
  • 原文地址:https://www.cnblogs.com/daxin/p/3560989.html
Copyright © 2011-2022 走看看