Rohit
Rohit

Reputation: 868

Using ehcache3 with Spring4

Can someone please help with creating an Ehcache backed CacheManager using Spring JCacheCacheManager via xml? I have something like this. Not sure how to create a javax.cache.CacheManager for Ehcache3.

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

thanks!

Upvotes: 4

Views: 5968

Answers (4)

cs_zineb
cs_zineb

Reputation: 33

I try this with spring 6 and ehcache 3, jdk17

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

but I have this error. I dont understand why it search a javax. knowning that I have the lib jakarta.xml.bind-api in the pom

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)

Upvotes: 0

Omid Ashouri
Omid Ashouri

Reputation: 123

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

Upvotes: 2

R.A
R.A

Reputation: 1871

@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);

  }

}

Upvotes: 2

Louis Jacomet
Louis Jacomet

Reputation: 14510

The recommended approach for doing this would be to use the org.springframework.cache.jcache.JCacheManagerFactoryBean in which you can inject a URI, Properties and ClassLoader. This factory bean will then use the standard JCache Caching class to create the javax.cache.CacheManager.

For Ehcache, the URI is used to point to an ehcache.xml that will then configure the CacheManager.

So expanding on your sample config:

<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>

For more information on Ehcache 3 / JCache integration, see the documentation.

Upvotes: 12

Related Questions