java - 基于 Spring 的单个 ehcache 管理器,用于多模块项目中的多个 ehcache.xml 文件

标签 java spring caching ehcache spring-cache

我有一个多模块项目,所有模块都在自己的 ehcache.xml 中定义其缓存配置。 现在未维护的“ehcache-spring-annotations”项目通过如下配置解决了此用例:

    <ehcache:annotation-driven cache-manager="ehCacheManager" create-missing-caches="true"/>

    <bean id="ehCacheManager" class="net.sf.itcb.addons.cachemanager.ItcbEhCacheManagerFactoryBean">
      <property name="configLocations" value="classpath*:ehcache.xml"/>
      <property name="shared" value="true"/>
    </bean>

我在 Spring 的缓存抽象中尝试了类似的方法。

    <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"/>

但是,我遇到了这个异常:

Caused by: java.io.FileNotFoundException: 
  class path resource [classpath*:ehcache.xml] cannot be opened because
  it does not exist

有人可以解释一下在多模块项目中为 EhCache 配置 Spring 缓存抽象的正确方法是什么吗?

最佳答案

如果您希望在单个 Spring 上下文中使用多个 CacheManager,则必须定义多个 bean 并向它们添加限定符,以便能够根据某些上下文区分它们。

所以这里没有什么具体的缓存,它是 Spring 中相同类型的经典多个 bean。

关于java - 基于 Spring 的单个 ehcache 管理器,用于多模块项目中的多个 ehcache.xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460488/

相关文章:

Java项目-Velocity不替换某些字符串

java - 通过 Spring Social InsufficientPermissionException 发布到 facebook 墙

asp.net-mvc - 我可以在 ASP.NET MVC 中使用 [CompressFilter] 而不破坏 donut 缓存吗

java - 模拟 Vertx.io 异步处理程序

java - 为什么不直接使用实例字段,而是赋值给一个局部变量呢?

java - 删除不适用于 HTC 的通话记录

java - Spring + 2 Hibernate 数据源 + 第二个 TransactionManager : NoSuchBeanDefinitionException

java - 为什么我使用 Spring Integration/JtaTransactionManager 看到多个事务

sql-server - Memcached 与 SQL Server 缓存

ios - Swift 数据缓存 - 最佳实践