Deniz Engin
Deniz Engin

Reputation: 35

Two CacheManagers overriding each other

I have a JCacheCacheManager injected into my application by a spring boot starter. This works fine until i define an ehCacheCacheManager-Bean inside the same app, which is used in the @cacheable-annotation of a single repository method. This itself works as well, but the injected JCacheCacheManager can not be found any more.

Upvotes: 0

Views: 883

Answers (2)

Deniz Engin
Deniz Engin

Reputation: 35

I did it slightly different: I changed the @Cacheable-Annotations referencing a custom CacheResolver Bean witch uses the EhCache. The EhCache itself is no bean anymore. Conflict solved.

Upvotes: 0

Avnish
Avnish

Reputation: 1321

That's an expected behavior in spring boot for almost everything in general. Spring boot provides opinionated beans with provided (or implicit if not provided) configuration. The moment spring boot see any of bean being explicitly provided by user, spring boot will stop auto providing it's opinionated bean. That's the whole purpose of AutoConfiguration.

In order for you to work with both cache managers, use following outline

  1. Provide an explicit bean for JCacheCacheManager yourself, mark it as @Primary - this bean will be injected by default everywhere as long as @Qualifier is not used asking for specific bean.
  2. Provide second bean of type ehCacheCacheManager, specify a bean name as well with this bean, say @Bean('ehCacheManager')
  3. Specifify the cache name explicitly where you want to use ehCacheManager, say by using @Cacheable(cacheManager="ehCacheManager" ...)

Upvotes: 2

Related Questions