在 Spring Boot 2.2.x 上执行多个 mongo 时,没有类型 '....mongodb.core.convert.MappingMongoConverter' 的合格 bean 可用
No qualifying bean of type '....mongodb.core.convert.MappingMongoConverter' available when doing multiple mongo on Spring Boot 2.2.x
所以我正在关注这个 this guide to setup my Spring boot application that uses multiple MongoDB Instances (multiple Mongo Instances) everything works well until I use the @EnableMongoAuditing
annotation. I've looked up this error and found a solution
You need to pipe the MappingMongoConverter that's available in the environment into MongoTemplate as well, i.e. use new MongoTemplate(dbFactory, converter). The constructor you use is for convenience, one-off usages. We usually recommend to use AbstractMongoConfiguration in case you'd like to customize anything MongoDB specific as this makes sure the components are wired together correctly.
我不能使用 AbstractMongoConfiguration
class 因为那应该是针对单个实例的 MongoDB.
我只使用 @CreatedDate
和 @LastModifiedData
的审核功能。
目前我正在像这样初始化 MappingMongoConverter
@Bean(name = SecondaryMongoConfig.MONGO_TEMPLATE)
public MongoTemplate secondaryMongoTemplate() throws Exception {
MongoDbFactory factory = secondaryFactory(this.mongoProperties.getSecondary());
MongoMappingContext mctx = new MongoMappingContext();
mctx.initialize();
DbRefResolver dbRef = new DefaultDbRefResolver(factory);
MappingMongoConverter mconv = new MappingMongoConverter(dbRef, mctx);
mconv.afterPropertiesSet();
return new MongoTemplate(primaryFactory(this.mongoProperties.getSecondary()),mconv);
}
但是出现同样的错误Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
我的问题可能是如何正确设置 MappingMongoConverter
以修复上述错误
我通过在 MultipleMongoConfig
class 文件
中定义以下 bean 解决了这个问题
@Bean
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
MongoMappingContext context, BeanFactory beanFactory)
{
context.setInitialEntitySet(new HashSet<>(Arrays.asList(Person.class, Animal.class,
Program.class)));
context.initialize();
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
try {
mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
} catch (NoSuchBeanDefinitionException ignored) {} // Swallow error
return mappingConverter;
}
@Bean
public MongoProperties mongoProperties() {
// not exactly sure which property should I return
return this.mongoProperties.getSecondary();
}
所以我正在关注这个 this guide to setup my Spring boot application that uses multiple MongoDB Instances (multiple Mongo Instances) everything works well until I use the @EnableMongoAuditing
annotation. I've looked up this error and found a solution
You need to pipe the MappingMongoConverter that's available in the environment into MongoTemplate as well, i.e. use new MongoTemplate(dbFactory, converter). The constructor you use is for convenience, one-off usages. We usually recommend to use AbstractMongoConfiguration in case you'd like to customize anything MongoDB specific as this makes sure the components are wired together correctly.
我不能使用 AbstractMongoConfiguration
class 因为那应该是针对单个实例的 MongoDB.
我只使用 @CreatedDate
和 @LastModifiedData
的审核功能。
目前我正在像这样初始化 MappingMongoConverter
@Bean(name = SecondaryMongoConfig.MONGO_TEMPLATE)
public MongoTemplate secondaryMongoTemplate() throws Exception {
MongoDbFactory factory = secondaryFactory(this.mongoProperties.getSecondary());
MongoMappingContext mctx = new MongoMappingContext();
mctx.initialize();
DbRefResolver dbRef = new DefaultDbRefResolver(factory);
MappingMongoConverter mconv = new MappingMongoConverter(dbRef, mctx);
mconv.afterPropertiesSet();
return new MongoTemplate(primaryFactory(this.mongoProperties.getSecondary()),mconv);
}
但是出现同样的错误Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
我的问题可能是如何正确设置 MappingMongoConverter
以修复上述错误
我通过在 MultipleMongoConfig
class 文件
@Bean
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
MongoMappingContext context, BeanFactory beanFactory)
{
context.setInitialEntitySet(new HashSet<>(Arrays.asList(Person.class, Animal.class,
Program.class)));
context.initialize();
DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
try {
mappingConverter.setCustomConversions(beanFactory.getBean(CustomConversions.class));
} catch (NoSuchBeanDefinitionException ignored) {} // Swallow error
return mappingConverter;
}
@Bean
public MongoProperties mongoProperties() {
// not exactly sure which property should I return
return this.mongoProperties.getSecondary();
}