14

I'm trying to get Ehcache 3 working with Spring 4 without using Spring boot.

Here is a working example out there which uses Spring Boot, but I'm working on an existing application which is not using Spring Boot.

The problem is that spring-context-support (which adds Spring's cache annotations) expects the Ehcache's CacheManager to be on this classpath: net.sf.ehcache.CacheManager

However, in Ehcache 3, the CacheManager class resides on another classpath: org.ehcache.CacheManager.

So, basically spring-context-support does not support Ehcache 3. And you would have to use the JSR-107 annotations directly, not the annotations provided by Spring.

But apparently it works with Spring Boot. Perhaps there is a way to make it work with a standard Spring Application as well. That's what I'm hoping. I really want to be using Spring's own annotations instead of the JSR-107 annotations.

D. Spreuer
  • 47
  • 8
Håvard Geithus
  • 5,544
  • 7
  • 36
  • 51
  • Use the proper ehcahce 3 version, the one that is supported. – M. Deinum Sep 08 '16 at 09:11
  • Which one is that? Thanks! – Håvard Geithus Sep 08 '16 at 09:26
  • 1
    Just readig it again. When using ehcache3 you have to use the jcache abstraction with Spring (you still can use the spring annotation but for configuration you have to use the jcache factories). Else use ehcache2. My mistake sorry. – M. Deinum Sep 08 '16 at 09:43
  • Thanks! I'll try that. I'll let you know later how it goes. – Håvard Geithus Sep 08 '16 at 10:11
  • Interested to know where you got the link to the article? It can still be reached from http://www.ehcache.org/blog/ but there seems to be a 'date' difference in the URL for some reason ... – Louis Jacomet Sep 09 '16 at 08:13
  • @LouisJacomet I got the article link from a google search two days ago (September 7). Then when I accessed it the next day, it was broken. (Double checked my browser's history) – Håvard Geithus Sep 09 '16 at 09:54

2 Answers2

15

Indeed there is no native support of Ehcache 3 in Spring Caching.

The good news is that you achieve what you want with the JCache support that Spring Caching has since Ehcache 3 is a compliant JCache implementation. And once you have a JCache CacheManager available in your application context, nothing forces you to use the JCache annotations. You can keep using the Spring Caching ones without any problem.

You can find a demo of that here.

Note: I am working on Ehcache

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
  • 1
    Thanks for clearing things up, that talk was really helpful! One of the things I did wrong in my setup was typing my caches in my Ehcache 3 configuration. Then when Spring's JCacheCacheManager called getCache(cacheName) on the underlying CacheManager (in my case Eh107CacheManager), it ended up throwing IllegalArgumentException: Cache [cacheName] specifies key/value types. Use getCache(String, Class, Class). – Håvard Geithus Sep 09 '16 at 09:44
  • 1
    +1, I also encounter this problem, I think this is a bug(or maybe a not supported feature) that use Ehcache3.x in Spring, we can't define the "key-type" and "value-type" property in the ehcache.xml – Rocky Hu Dec 02 '16 at 06:44
  • 2
    Is it possible to use Ehcache3 with Spring ACL ? – Sharadr Mar 02 '18 at 20:07
  • @Sharadr have you tried with spring ACL? Did it worked? – Thirumal Feb 24 '20 at 14:27
4

This is simple and working sample for all needed changes from ehcache 2 to 3: https://imhoratiu.wordpress.com/2017/01/26/spring-4-with-ehcache-3-how-to/

This this is link for new configuration:

Grigory Kislin
  • 16,647
  • 10
  • 125
  • 197