Spring 4.2.4(不使用spring boot)+ EhCache 3 + Hibernate 4.2.1

5

有没有人在Spring 4.2中实现过EhCache 3(不使用Spring Boot)?如果有,那么实现的步骤是什么?

问题在于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配置以供参考。

2个回答

5

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控制台时,在启动期间出现了以下异常。Caused by: java.lang.UnsupportedClassVersionError: JVMCFRE003 bad major version; class=org/ehcache/jsr107/EhcacheCachingProvider, offset=6. Websphere版本为8.5.5.9,其中JDK版本为1.7.1.64。 - Mohan
我遇到了这个错误: 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
@wonsuc 这个错误跟这个问题没有太大关系。你应该创建自己的问题。但是这意味着你的 ehcache.xml 格式不正确。 - Henri

0
我建议您依赖于JSR-107(也称为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
我邀请您访问上面的博客文章和链接的文档,您的用例非常标准且得到了很好的支持。如有进一步问题,请随时提问。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接