Closed kewshah4 closed 7 months ago
The documentation says:
If your project creates custom CacheManager beans, it will either need to be marked with
@Primary
or injected using@Qualifier.
The error you posted seems to be caused by your own code. Your need to put @Qualifier
where you inject the bean (MyCache in your case).
That said, I'm getting a similar error in Spring's code, so adding @Qualifier
doesn't help me. I have 2 custom CacheManager beans, and I have to add @Primary
to the first one to avoid this error:
java.lang.IllegalStateException: No CacheResolver specified, and no unique bean of type CacheManager found. Mark one as primary or declare a specific CacheManager to use.
at org.springframework.cache.interceptor.CacheAspectSupport.afterSingletonsInstantiated(CacheAspectSupport.java:223)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:972)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:584)
at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:66)
It should also be noted that the Spring Cloud Gateway (3.0.1) included in spring-cloud 2022.0.1 has a bug where LocalResponseCacheAutoConfiguration is enabled by default, otherwise the gatewayCacheManager bean wouldn't exist to begin with. You can override the gateway version for now:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway-dependencies</artifactId>
<version>4.0.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2022.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I managed to make it work, doing:
@EnableCaching
LocalResponseCacheAutoConfiguration
:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
</dependencies>
Describe the bug
I am using
spring-boot
3.0.2
andspring-cloud
2022.0.1
. I am injecting theCacheManager
as a@Bean
method parameter, for examplethis results in a conflict with the
CacheManager
bean now being created inLocalResponseCacheAutoConfiguration
.I tried adding
@Qualifier("cacheManager")
to the@Bean
method parameter, but it didn't work.Sample https://github.com/kewshah4/cache-bug
Possibly related to
2841