rchz
rchz

Reputation: 23

How to setup two different CacheManager in spring-boot

I'm trying to setup a spring-boot application with two CacheManagers, with code as below:

@SpringBootApplication
@EnableCaching
public class TestApplication {
...
}

@Configuration
public class TestGuavaCacheConfig extends CachingConfigurerSupport {
...
}

@Configuration
public class TestRedisCacheConfig extends CachingConfigurerSupport {
...
}

But when I start the app, it always fails with the following error:

Caused by: java.lang.IllegalStateException: 2 implementations of CachingConfigurer were found when only 1 was expected. Refactor the configuration such that CachingConfigurer is implemented only once or not at all. at org.springframework.cache.annotation.AbstractCachingConfiguration.setConfigurers(AbstractCachingConfiguration.java:71) ~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_66] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_66] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_66] at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_66] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:654) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]

... 59 common frames omitted

It seems that spring-boot can't support two CacheManager(s). Is this true ?

Upvotes: 2

Views: 7181

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33091

TL;DR CachingConfigurer is meant to configure the default cache settings.

This has nothing to do with Spring Boot, that interface (and the related exception) comes straight from Spring Framework.

CachingConfigurer allows you to specify the default CacheManager that your application should be using. As the exception states, you can't have two of them. That does not mean you can't have two cache managers of course.

What are you trying to do exactly? If you want to define two cache managers and use the cacheManager attribute of the @CacheConfig or @Cacheable annotations, then your (only) CacheConfigurer implementation should define the default one and you should create the other like any other bean that you'll reference in the annotation.

If you want to switch from one cache to the other, consider implementing the CacheResolver instead and wrap your CacheManager instances in it. Based on a custom annotation and/or a cache name, you'll be able to return the cache(s) to use with some custom code of yours.

Upvotes: 4

Related Questions