【问题标题】:Single Spring based ehcache manager for multiple ehcache.xml files in a multi module project单个基于 Spring 的 ehcache 管理器,用于多模块项目中的多个 ehcache.xml 文件
【发布时间】:2017-03-20 12:28:17
【问题描述】:

我有一个多模块项目,所有模块都在自己的 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 Abstraction 中尝试过类似的东西。

    <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 项目中将此作为issue 提出。

标签: java spring caching ehcache spring-cache


【解决方案1】:

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

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

【讨论】:

  • 嗨@Louis Jacomet,感谢您的建议。我有近 20 多个模块,它们都有一个 ehcache.xml 文件。我使用的当前解决方案将所有这些 ehcache.xml 文件合并到单个输入流中并创建单个缓存管理器。我想知道 Spring 是否支持开箱即用。
【解决方案2】:

最后,我们离开了 ehcache.xml,开始为我们的缓存使用基于 Java 的配置,从而解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多