Closed chandrasekhar4u closed 7 years ago
Please use StackOverflow or our mailing list to ask such questions.
Spring supports Ehcache 2 and 3. The EhCacheCacheManager
is used for Ehcache 2. You should not use it. Ehcache 3 is configured through JCache so you should use the JCacheCacheManager
instead.
Have a look here to see an example.
If I used ehcache.xml in the application still I need to programatically create cache manager bean and need to create cache objects?
@chandrasekhar4u you can use either jsr-107 api or ehcache3 api to load an ehcache.xml and retrieve your CacheManager and Caches from it : http://www.ehcache.org/documentation/3.3/getting-started.html http://www.ehcache.org/documentation/3.3/107.html
To use only configuration you can do something similar to:
application.properties
spring.cache.jcache.config=ehcache.xml
App.java:
@SpringBootApplication
@EnableCaching
public class App
{
public static void main( String[] args ) throws Exception {
SpringApplication.run(App.class, args);
}
}
Assuming you have spring-boot-starter-cache
as a dependency.
The JCacheCacheConfiguration
class will magically retrieve the ehcache.xml
using the property and seeing the Ehcache is the JCache provider, initialize everything correctly.
@henri-tremblay Thank you for the reply,
I tried the same, and I autowired cacheManager in one of my spring service.
While application deployment I can see cache is getting loaded, but when I try to access pages, it's showing error Unsatisfied dependency expressed through field 'cacheManager'
.
How can I access cacheManager and cache objects from it? (how application will come to know which cache manager implementation to invoke?)
You can use @Cacheable
annotation to cache a method. But autowiring should work. Make sure you are autowiring the spring cache manager (org.springframework.cache.CacheManager
), not the Ehcache one.
If that's already the case, I would need a full stack trace to help you.
That's the key for me:
Make sure you are autowiring the spring cache manager (org.springframework.cache.CacheManager)
Thanks @henri-tremblay.
I am using Spring boot 1.5.2.RELEASE, Spring 4.3.7, ehcache 3.3.1 in my application,
ehcache.xml
application.properties
spring.cache.jcache.config=classpath:ehcache.xml spring.cache.cache-names=mayCache1,mayCache2,mayCache3 spring.cache.caffeine.spec=maximumSize=1000,expireAfterAccess=600s
pom.xml dependencies:
When I try to access cache manager in CacheConfiguration class it's throwing error like below:
Unsatisfied dependency expressed through field 'cacheManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.cache.CacheManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
While debugging I observed that EhCacheCacheManager is getting called and trying to autowire net.sf.ehcache.CacheManager cacheManager from it. I am not able to understand why spring 4.3.x has old version of CacheManager instance of Ehcache, and not able to inject/autowire object. Please help me to solve this issue.