【问题标题】:How to configure Jcache with Ecache as Provider in Spring application-context.xml?如何在 Spring application-context.xml 中使用 Ecache 作为 Provider 配置 Jcache?
【发布时间】:2015-06-25 13:32:43
【问题描述】:

Spring 文档提供了以下信息。

<bean id="cacheManager"
   class="org.springframework.cache.jcache.JCacheCacheManager"
   p:cache-manager-ref="jCacheManager"/>

<!-- JSR-107 cache manager setup  -->
<bean id="jCacheManager" .../>

我想确切地知道如何在 spring 应用程序上下文 xml 中配置这个 jcacheManager bean(以 EhCache 作为提供者)。

我已经在 pom.xml 中配置了依赖项,如下所示。

<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>jcache</artifactId>
    <version>1.0.1</version>
    <exclusions>
        <exclusion>
            <artifactId>slf4j-api</artifactId>
            <groupId>org.slf4j</groupId>
        </exclusion>
    </exclusions>
</dependency> 

【问题讨论】:

    标签: spring ehcache spring-cache jcache


    【解决方案1】:

    这真的取决于你想如何配置它。如果您使用的是 Spring Boot 1.3,它将自动为您创建。或许你可以看看JCacheCacheConfiguration的来源?

    您可以通过Caching.getCachingProvider().getCacheManager()检索默认 javax.cache.CacheManager

    【讨论】:

      【解决方案2】:

      我们现在把Ehache3.x和Spring4.x集成起来不太方便。 Spring boot 做到了,它重写了一些代码:

      <bean id="cacheManager"
         class="org.springframework.cache.jcache.JCacheCacheManager"
      

      在 Spring Boot 中,它是:

      @Bean
          public JCacheCacheManager cacheManager(CacheManager jCacheCacheManager) {
              return new JCacheCacheManager(jCacheCacheManager);
          }
      

      它需要一个 javax.cache.CacheManager 实例,

      <!-- JSR-107 cache manager setup  -->
      <bean id="jCacheManager" .../>
      

      Ehcache 没有给我们深入介绍。

      Spring boot 喜欢:

      @Bean
          @ConditionalOnMissingBean
          public CacheManager jCacheCacheManager() throws IOException {
              CacheManager jCacheCacheManager = createCacheManager();
              List<String> cacheNames = this.cacheProperties.getCacheNames();
              if (!CollectionUtils.isEmpty(cacheNames)) {
                  for (String cacheName : cacheNames) {
                      jCacheCacheManager.createCache(cacheName, getDefaultCacheConfiguration());
                  }
              }
              customize(jCacheCacheManager);
              return jCacheCacheManager;
          }
      

      创建javax.cache.CacheManager是正常操作,跟Ehcache文件一样。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-31
        • 1970-01-01
        • 2012-09-24
        • 2015-05-29
        • 1970-01-01
        • 1970-01-01
        • 2015-06-01
        • 1970-01-01
        相关资源
        最近更新 更多