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

标签 spring ehcache ehcache-3

有没有人用 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 配置以供引用。

最佳答案

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++;
    }

}

关于Spring 4.2.4(不使用 spring boot)+ EhCache 3 + Hibernate 4.2.1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999986/

相关文章:

spring - SpEL - 空值比较

结合远程 Terracotta 配置的 Grails 本地 ehcache

java - 当通过sql过程发生表更新时更新spring缓存

java - Tomcat集群的优缺点

java - Ehcache无法使用java中的Generic类进行初始化

java - ehCache缓存有自动刷新的选项吗?没有任何调度程序工作?

spring - 为什么我的代码会跳过空白字段?

java - 无法使用 POSTMAN 删除(映射)我的数据库的对象

java - 为什么在使用 @RunWith(SpringJUnit4ClassRunner.class) 时,JUnit 4.12 会针对 assertEquals 抛出 NoSuchMethodError?