将ehcache3与Spring4结合使用

问题描述 投票:0回答:4

有人可以帮助通过 xml 使用 Spring

CacheManager
创建支持 Ehcache 的
JCacheCacheManager
吗?我有这样的东西。不知道如何为 Ehcache3 创建
javax.cache.CacheManager

<bean id="myCacheManager"
    class="org.springframework.cache.jcache.JCacheCacheManager">
    <property name="cacheManager" value="..." />
</bean>

谢谢!

spring ehcache
4个回答
12
投票

建议的方法是使用

org.springframework.cache.jcache.JCacheManagerFactoryBean
,您可以在其中注入
URI
Properties
ClassLoader
。然后,该工厂 bean 将使用标准 JCache
Caching
类来创建
javax.cache.CacheManager

对于 Ehcache,

URI
用于指向
ehcache.xml
,然后配置
CacheManager

因此扩展您的示例配置:

<bean id="jCacheManager" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
  <property name="cacheManagerUri" value="file://path/to/ehcache.xml"/>
</bean>
<bean id="myCacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
  <property name="cacheManager" ref="jCacheManager" />
</bean>

有关 Ehcache 3 / JCache 集成的更多信息,请参阅 文档


2
投票
@Configuration
@EnableCaching
public class CacheConfig {

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

  @Bean(destroyMethod = "close")
  public javax.cache.CacheManager cacheManager() throws IOException {
    XmlConfiguration xmlConfig = new XmlConfiguration(new ClassPathResource("ehcache.xml").getURL());
    EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
    return provider.getCacheManager(provider.getDefaultURI(), xmlConfig);

  }

}

2
投票
@Configuration
@EnableCaching
public class CacheConfig {

    @Bean
    public JCacheCacheManager jCacheCacheManager(JCacheManagerFactoryBean jCacheManagerFactoryBean){
        JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
        jCacheCacheManager.setCacheManager(jCacheManagerFactoryBean.getObject());
        return jCacheCacheManager;
    }

    @Bean
    public JCacheManagerFactoryBean jCacheManagerFactoryBean() throws URISyntaxException {
        JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
        jCacheManagerFactoryBean.setCacheManagerUri(getClass().getResource("/ehcache.xml").toURI());
        return jCacheManagerFactoryBean;
    }
}

0
投票

我用 spring 6 和 ehcache 3、jdk17 尝试这个

 <bean id="ehcache" class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
        <property name="cacheManagerUri" value="classpath:ehcache.xml"/>
 </bean>

但是我有这个错误。我不明白为什么它搜索 javax.知道我在 pom 中有 lib jakarta.xml.bind-api

Caused by: java.lang.NoClassDefFoundError: javax/xml/bind/ValidationEventHandler
    at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:116)
    at org.ehcache.xml.XmlConfiguration.<init>(XmlConfiguration.java:92)
    at org.ehcache.jsr107.EhcacheCachingProvider$ConfigSupplier.getConfiguration(EhcacheCachingProvider.java:328)
    at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:134)
    at org.ehcache.jsr107.EhcacheCachingProvider.getCacheManager(EhcacheCachingProvider.java:85)
    at org.springframework.cache.jcache.JCacheManagerFactoryBean.afterPropertiesSet(JCacheManagerFactoryBean.java:83)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1817)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1766)
© www.soinside.com 2019 - 2024. All rights reserved.