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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值