【问题标题】:Spring 4.2.4 (not using spring boot) + EhCache 3 + Hibernate 4.2.1Spring 4.2.4(不使用 Spring Boot)+ EhCache 3 + Hibernate 4.2.1
【发布时间】:2018-02-27 02:04:21
【问题描述】:

是否有人使用 Spring 4.2(不使用 Spring boot)实现了 EhCache 3。如果是这样,实现它的步骤是什么?

问题在于 spring-context-support(它添加了 Spring 的缓存注释)期望 Ehcache 的 CacheManager 在这个类路径上:net.sf.ehcache.CacheManager

但是,在 Ehcache 3 中,CacheManager 类驻留在另一个类路径中:org.ehcache.CacheManager。

所以,基本上 spring-context-support 不支持 Ehcache 3。你必须直接使用 JSR-107 注释,而不是 Spring 提供的注释。

如果有人实现了这个组合,请给你ehcache.xml和spring配置供参考。

【问题讨论】:

    标签: spring ehcache ehcache-3


    【解决方案1】:

    Ehcache 3 通过 JSR-107 使用。这是一个例子。

    你的pom.xml

    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>4.2.9.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>3.5.0</version>
      </dependency>
      <dependency>
        <groupId>javax.cache</groupId>
        <artifactId>cache-api</artifactId>
        <version>1.1.0</version>
      </dependency>
    

    您的ehcache.xml(位于类路径的根目录):

    <?xml version="1.0" encoding="UTF-8"?>
    <config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
        xmlns='http://www.ehcache.org/v3'
        xsi:schemaLocation="
            http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.4.xsd
            http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.4.xsd">
    
      <service>
        <jsr107:defaults enable-management="false" enable-statistics="true"/>
      </service>
    
      <cache alias="cache">
        <resources>
          <heap unit="entries">2000</heap>
        </resources>
      </cache>
    </config>
    

    使用缓存的示例应用:

    import org.springframework.cache.CacheManager;
    import org.springframework.cache.annotation.Cacheable;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cache.jcache.JCacheCacheManager;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import java.net.URISyntaxException;
    
    import javax.cache.Caching;
    
    @EnableCaching
    @Configuration
    public class App {
    
        private static int value = 0;
    
        @Bean
        public CacheManager cacheManager() throws URISyntaxException {
            return new JCacheCacheManager(Caching.getCachingProvider().getCacheManager(
                getClass().getResource("/ehcache.xml").toURI(),
                getClass().getClassLoader()
            ));
        }
    
        public static void main( String[] args ) {
            ApplicationContext context = new AnnotationConfigApplicationContext(App.class);
    
            App app = context.getBean(App.class);
            System.out.println(app.incValue());
            System.out.println(app.incValue()); // still return 0
        }
    
        @Cacheable("cache")
        public int incValue() {
            return value++;
        }
    
    }
    

    【讨论】:

    • 它在本地运行良好,但是当我将更改部署到 Websphere 控制台时,我在启动期间遇到此异常。引起:java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=org/ehcache/jsr107/EhcacheCachingProvider,偏移量=6。 Websphere版本是8.5.5.9,JDK版本是1.7.1.64。
    • 我收到此错误。 Invalid content was found starting with element '{"http://www.ehcache.org/v3":service}'. One of '{"http://www.ehcache.org/v3":thread-pools, "http://www.ehcache.org/v3":event-dispatch, "http://www.ehcache.org/v3":write-behind, "http://www.ehcache.org/v3":heap-store, "http://www.ehcache.org/v3":disk-store, "http://www.ehcache.org/v3":cache, "http://www.ehcache.org/v3":cache-template}' is expected.
    • @wonsuc 与此错误无关。您应该创建自己的问题。但这意味着您的 ehcache.xml 格式不正确。
    【解决方案2】:

    我建议您依赖 JSR-107(又名 JCache,用于在 JVM 上进行缓存的标准 API)Spring 支持,然后在类路径中添加 ehcache3。

    您也可以使用 Spring 自己的注解,它与 JSR 107 的集成非常好:Spring 已经支持 JSR-107 将近 4 年了:https://spring.io/blog/2014/04/14/cache-abstraction-jcache-jsr-107-annotations-support

    我邀请您访问上面的博客文章及其链接的文档,您的用例非常标准且非常受支持。欢迎提出更多问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-14
      • 2017-01-16
      • 2011-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多