升级到 spring-boot 3 后,使用连接字符串时,mongo 自动配置不起作用。
我认为如果为空,我们需要有一个后备选项this.connectionDetails.getConnectionString().getDatabase()
。
@Bean
@ConditionalOnMissingBean(ReactiveMongoDatabaseFactory.class)
public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoClient mongo) {
return new SimpleReactiveMongoDatabaseFactory(mongo,
this.connectionDetails.getConnectionString().getDatabase());
}
我们可以将上面的代码更改为下面提到的吗?
@Bean
@ConditionalOnMissingBean(ReactiveMongoDatabaseFactory.class)
public SimpleReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory(MongoProperties properties, MongoClient mongo) {
String database = this.connectionDetails.getConnectionString().getDatabase();
if(database==null)
database = properties.getDatabase()
return new SimpleReactiveMongoDatabaseFactory(mongo,
database);
}