0

I am using

@Service
public class Service{
@Autowired  
private CacheManager cacheManager;
}  

I included org.ehcache:ehcache and spring-boot-starter-cache library in the build.gradle file. on running the application I am getting the error:
Field cacheManager required a bean of type org.springframework.cache.CacheManager that could not be found.
I am not sure how to go about resolving this error

My Thoughts:
Looks like I need to declare a class annotated with @Configuration and with methods @Bean which returns an object of type CacheManager. I am using EhCache here. Not sure exactly how to do this.

John Blum
  • 7,381
  • 1
  • 20
  • 30
juan
  • 47
  • 7

1 Answers1

0

I believe @dey is correct.

I suspect the problem you are having is that you have not explicitly enabled caching using Spring Framework's @EnableCaching annotation (Javadoc).

Even with Spring Boot's auto-configuration, and specifically when using Ehcache as a caching provider in Spring Framework's Cache Abstraction, you still need to explicitly enable caching, as described in the documentation.

Ehcache is a supported caching provider (as well as in the core Spring Framework) and Spring Boot does offer auto-configuration logic for this provider (via the JCache API). However, when using Spring Boot, an explicit CacheManager bean declaration (for Ehcache) in your application configuration is not required, only the @EnableCaching annotation is required (so long as both Ehcache and spring-boot-starter-cache are on your application classpath, as you have indicated):

@Configuration
@EnableCaching
class MySpringBootApplicationConfiguration {

  // your application managed bean declarations here

}

An explicit CacheManager bean is required only when 1) you are not using Spring Boot, or 2) Spring does not (provide) support the provider as a caching provider in Spring's Cache Abstraction, in which case you would need to implement Spring's Cache and CacheManager` SPI yourself, for the unsupported (OOTB) provider.

John Blum
  • 7,381
  • 1
  • 20
  • 30