如何在 spring-boot 中设置两个不同的 CacheManager [英] How to setup two different CacheManager in spring-boot

查看:96
本文介绍了如何在 spring-boot 中设置两个不同的 CacheManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个 CacheManager 设置一个 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个实现CachingConfigurer 在预期只有 1 时被发现.重构配置使得 CachingConfigurer 只实现一次或一点也不.在org.springframework.cache.annotation.AbstractCachingConfiguration.setConfigurers(AbstractCachingConfiguration.java:71)~[spring-context-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)~[na:1.8.0_66] 在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)~[na:1.8.0_66] 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)~[na:1.8.0_66] 在 java.lang.reflect.Method.invoke(Method.java:497)~[na:1.8.0_66] 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:654)~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] 在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]

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 个常用帧

... 59 common frames omitted

spring-boot 似乎不能支持两个 CacheManager(s).这是真的吗?

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

推荐答案

TL;DR CachingConfigurer 旨在配置默认缓存设置.

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

这与 Spring Boot 无关,该接口(以及相关异常)直接来自 Spring Framework.

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

CachingConfigurer 允许您指定应用程序应该使用的默认 CacheManager.作为例外状态,您不能拥有其中两个.当然,这并不意味着您不能拥有两个缓存管理器.

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.

你到底想做什么?如果您想定义两个缓存管理器并使用 @CacheConfig@Cacheable 注释的 cacheManager 属性,那么您的(仅)CacheConfigurer 实现应该定义默认的一个,你应该像在注释中引用的任何其他 bean 一样创建另一个.

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.

如果您想从一个缓存切换到另一个缓存,请考虑实施 CacheResolver 并将您的 CacheManager 实例包装在其中.根据自定义注释和/或缓存名称,您将能够返回缓存以与您的某些自定义代码一起使用.

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.

这篇关于如何在 spring-boot 中设置两个不同的 CacheManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆