springehcache

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!


Solution

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