Spring Boot 2.1重复的reactMongoTemplate bean [英] Spring Boot 2.1 duplicate reactiveMongoTemplate bean
问题描述
我有以下Spring Boot @Configuration
类:
I have the following Spring Boot @Configuration
class:
@Configuration
@EnableReactiveMongoRepositories
class MongoConfiguration : AbstractReactiveMongoConfiguration()
{
override fun reactiveMongoClient() = MongoClients.create()
override fun getDatabaseName() = "mydb"
override fun customConversions(): MongoCustomConversions =
MongoCustomConversions(listOf(ZonedDateTimeReadConverter(), ZonedDateTimeWriteConverter()))
}
应用程序无法启动,并记录以下消息:
The application fails to start, and logs this message:
无法注册在类路径资源[org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.class]中定义的bean"reactiveMongoTemplate".具有该名称的Bean已在类路径资源[com/mypackage/MongoConfiguration.class]中定义,并且禁止覆盖.
The bean 'reactiveMongoTemplate', defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [com/mypackage/MongoConfiguration.class] and overriding is disabled.
这使我感到困惑,因为MongoReactiveDataAutoConfiguration
中的reactiveMongoTemplate
ben方法是用@ConditionalOnMissingBean
配置的.
This puzzles me, as the reactiveMongoTemplate
ben method in MongoReactiveDataAutoConfiguration
is configured with @ConditionalOnMissingBean
.
推荐答案
问题出在您已子分类的AbstractReactiveMongoConfiguration
中.其reactiveMongoTemplate
@Bean
方法的签名指出它返回ReactiveMongoOperations
.在创建Bean之前,所有可用的类型信息都是如此,并且Bean工厂无法知道Bean实际上是ReactiveMongoTemplate
实例.结果,正在寻找ReactiveMongoTemplate
bean的@ConditionaOnMissingBean
找不到一个,因此尝试定义两个bean.应该在Spring Data MongoDB中修复此问题,以便AbstractReactiveMongoConfiguration
为它的bean提供尽可能多的类型信息.我已经打开 DATAMONGO-2355 .
The problem is in AbstractReactiveMongoConfiguration
that you have sub-classed. The signature of its reactiveMongoTemplate
@Bean
method states that it returns ReactiveMongoOperations
. Until the bean has been created, that's all the type information that is available and there is no way for the bean factory to know that the bean is actually a ReactiveMongoTemplate
instance. As a result, the @ConditionaOnMissingBean
that's looking for a ReactiveMongoTemplate
bean doesn't find one so an attempt to define both beans is made. This should be fixed in Spring Data MongoDB so that AbstractReactiveMongoConfiguration
provides as much type information as possible for its beans. I've opened DATAMONGO-2355.
您可以通过更多使用Spring Boot的自动配置来避免此问题.不必对AbstractReactiveMongoConfiguration
进行子分类,您可以:
You can avoid the problem by making more use of Spring Boot's auto-configuration. Rather than sub-classing AbstractReactiveMongoConfiguration
you can:
- 使用配置属性
spring.data.mongodb.database=mydb
设置数据库. - 使用自动配置的
MongoClient
bean,而不是定义自己的bean. - 定义您自己的
MongoCustomConversions
bean,然后将其用于Boot会自动配置的bean.
- Use the configuration property
spring.data.mongodb.database=mydb
to set the database. - Use the auto-configured
MongoClient
bean rather than defining your own. - Define your own
MongoCustomConversions
bean that will then be used in favour of one that Boot would otherwise auto-configure.
这篇关于Spring Boot 2.1重复的reactMongoTemplate bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!