Why would Spring Boot/EhCache NOT use the specified configuration file?

1.3k Views Asked by At

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.

0

There are 0 best solutions below