Spring Boot 2.1重复的reactMongoTemplate bean [英] Spring Boot 2.1 duplicate reactiveMongoTemplate bean

查看:267
本文介绍了Spring Boot 2.1重复的reactMongoTemplate 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:

  1. 使用配置属性spring.data.mongodb.database=mydb设置数据库.
  2. 使用自动配置的MongoClient bean,而不是定义自己的bean.
  3. 定义您自己的MongoCustomConversions bean,然后将其用于Boot会自动配置的bean.
  1. Use the configuration property spring.data.mongodb.database=mydb to set the database.
  2. Use the auto-configured MongoClient bean rather than defining your own.
  3. 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屋!

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