0

I'm trying to enable EhCache 3 caching inside a Spring Boot 2 application and as near as I can tell I've set this up correctly but caching is just...NOT working and I'm being given no information as to why.

I've added to my maven POM:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
    <groupId>org.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>
<dependency>
    <groupId>javax.cache</groupId>
    <artifactId>cache-api</artifactId>
</dependency>

I've added an @EnableCaching annotation. I've specified the config, I'm using bootstrap.yml so:

spring:
  cache:
    jcache:
      config: classpath:ehcache.xml

But just in case I also put the spring.cache.jcache.config version in a properties file.

I've written a basic src/main/resources/ehcache.xml file...

Finally, I've set a method as @Cacheable but it errors out when I call it with "Cannot find cache named..."

It literally doesn't matter one iota what I write in ehcache.xml. I can paste in Lorem Ipsum text instead of XML and have no errors or indication it even opened the file.

Spring Boot itself seems to have found the appropriate pre-requisites for JSR 107 autoconfiguration:

   JCacheCacheConfiguration matched:
      - @ConditionalOnClass found required classes 'javax.cache.Caching', 'org.springframework.cache.jcache.JCacheCacheManager' (OnClassCondition)
      - Cache org.springframework.boot.autoconfigure.cache.JCacheCacheConfiguration automatic cache type (CacheCondition)

I've tried going back to EhCache 2, so I replaced org.ehcache with net.sf.ehcache, switched my YML to ehcache instead of jcache and removed javax.cache. This puts this into my auto-configuration report:

   EhCacheCacheConfiguration matched:
      - @ConditionalOnClass found required classes 'net.sf.ehcache.Cache', 'org.springframework.cache.ehcache.EhCacheCacheManager' (OnClassCondition)
      - Cache org.springframework.boot.autoconfigure.cache.EhCacheCacheConfiguration automatic cache type (CacheCondition)
      - ResourceCondition (EhCache) found resource 'classpath:/ehcache.xml' (EhCacheCacheConfiguration.ConfigAvailableCondition)

But I still don't have any caches available to me. Why would Spring's autoconfiguration mechanism go so far as to mention that it found the EhCache 2 config file but not actually configure anything? Is there some other auto-configuration class I need to also look up in the report like a GenericCache, CacheConfiguration, something else?

What I can't see is any indication that it tried to do anything to start up a caching subsystem, read my configs, did anything.

As convenient as autoconfiguration is, when it doesn't feel like doing the thing it frustratingly tells me absolutely nothing about why it's deciding to blow me off so I could really use some help figuring out how to tease those details out of the thing.

Did I miss something? What can I debug and where can I look to figure out why it's ignoring the XML configuration I'm trying to tell it about? I tried adding some breakpoints to Spring CacheProperties around the jcache settings but it didn't hit them when I attached the debugger.

Sloloem
  • 1,587
  • 2
  • 15
  • 37
  • 1
    Your config is wrong. You are using EhCache and specify the parameters in the `application.yml` for JCache ( a different caching implementation) so no this will not work like this. Use the proper config parameter or the proper caching framework. And remove the `cache-api` dependency from your dependencies you only need ehcache. – M. Deinum Apr 22 '21 at 05:22
  • @M.Deinum I was under the impression from the documentation that for EhCache 2, IE net.sf.ehcache, you used "EhCache" in Spring but for EhCache 3, IE org.ehcache, you used JCache as in JSR107, is that really totally off? https://docs.spring.io/spring-boot/docs/2.5.x/reference/html/spring-boot-features.html#boot-features-caching-provider – Sloloem Apr 22 '21 at 16:57
  • You can use plain EhCache as wll. Nonetheless why are you using `bootstrap.yml` and not `application.yml`? The latter is used together with Spring Cloud Config to eagerly configure things for that. Without Spring Cloud Config that file isn't read (nor is a `bootstrap.properties`). So it looks like you are trying to use something that isn't available. If it is, it might be that your properties get overridden by the values gotten from the remote configuration. However I would strongly suggest to move them to `application.yml` and not `bootstrap.yml` as they serve different purposes. – M. Deinum Apr 23 '21 at 06:56
  • Well, I'm using Spring Cloud Config. spring-cloud-starter-config and all that. Should I split the properties up between the two and run with both files, and what would go in each? I know Spring is looking at bootstrap.yml because I can use it to turn debug on and off. Nothing in the remote config has any spring properties so it should be safe. I also have JMX and a bunch of Actuator configuration in bootstrap.yml and they seem to be working. – Sloloem Apr 23 '21 at 16:07
  • @M.Deinum, well, I tried going back down to EhCache 2 as well as described above and it even puts that it found the XML file in the auto-configuration report but it still won't setup my caches or tell me why. – Sloloem Apr 23 '21 at 20:28

0 Answers0