首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring 4.2.4 (不使用spring boot) + EhCache 3+ Hibernate 4.2.1

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

Stack Overflow用户
提问于 2018-02-27 10:04:22
回答 2查看 2.9K关注 0票数 4

有没有人用Spring4.2实现了EhCache 3(不使用Spring )。如果是这样的话,实现它的步骤是什么?

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

然而,在Ehcache3中,CacheManager类驻留在另一个类路径上: org.ehcache.CacheManager。

因此,spring-context-support基本上不支持Ehcache3,而且您必须直接使用JSR-107注释,而不是Spring提供的注释。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-27 22:39:39

Ehcache3通过JSR-107使用。下面是一个例子。

你的pom.xml

代码语言:javascript
复制
<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 (在类路径的根目录中):

代码语言:javascript
复制
<?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>

使用缓存的示例应用程序:

代码语言:javascript
复制
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++;
    }

}
票数 5
EN

Stack Overflow用户

发布于 2018-02-27 21:21:11

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

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

我邀请你访问上面的博客帖子和它链接的文档,你的用例是非常标准和非常受支持的。请随时提出进一步的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48999986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档