spring-bootehcache-3

why default CacheManager is not getting in spring boot 3 and ehcache 3


I'm using Spring Boot 3 to implement EhCache 3. It functions flawlessly in the spring boot embedded Tomcat server, however when I attempt to deploy it in the Tomcat server, the following problem is displayed.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class]: Unsatisfied dependency expressed through method 'cacheManager' parameter 1: Error creating bean with name 'jCacheCacheManager' defined in class path resource [org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.class]: Failed to instantiate [javax.cache.CacheManager]: Factory method 'jCacheCacheManager' threw exception with message: Cache 'cache' creation in EhcacheManager failed.

adding details of code

build.gradle implementation group: 'org.springframework.boot', name: 'spring-boot-starter-cache' implementation group: 'javax.cache', name: 'cache-api', version: '1.1.1' implementation group: 'org.ehcache', name: 'ehcache', version: '3.10.8'

application.properties spring.cache.jcache.config=classpath:ehcache.xml spring.cache.jcache.provider=org.ehcache.jsr107.EhcacheCachingProvider

also added @EnableCaching in Application class.

I'm getting the above error when I try to autowire CacheManager. Why is it not getting through? The CacheManager


Solution

  • By default ehcache uses javax.cache but with Spring Boot 3 you need to use jakarta instead of javax. There is a fix for this in Ehcache 3.10: https://github.com/ehcache/ehcache3/releases/tag/v3.10.0

    Remove:

    implementation group: 'javax.cache', name: 'cache-api', version: '1.1.1'
    

    Add classifier to ehcache dependency:

    implementation "org.ehcache:ehcache:3.10.8:jakarta"