问题描述
我如何自定义 MappingMongoConverter
在我的 Spring-Boot-Application (1.3.2.RELEASE) 中,无需更改由 spring-data 自动配置的任何 mongo-stuff?
How could I customize the MappingMongoConverter
within my Spring-Boot-Application (1.3.2.RELEASE) without changing any of the mongo-stuff which is autoconfigured by spring-data?
我目前的解决方案是:
@Configuration
public class MongoConfig {
@Autowired
private MongoDbFactory mongoFactory;
@Autowired
private MongoMappingContext mongoMappingContext;
@Bean
public MappingMongoConverter mongoConverter() throws Exception {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory);
MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
//this is my customization
mongoConverter.setMapKeyDotReplacement("_");
mongoConverter.afterPropertiesSet();
return mongoConverter;
}
}
这是正确的方法还是我会用这个破坏一些东西?
或者有没有更简单的方法来设置 mapKeyDotReplacement?
Is this the right way or do I break some stuff with this?
Or is there even a more simple way to set the mapKeyDotReplacement?
推荐答案
这是正确的方法.自动配置的 MappingMongoConverter
用 @ConditionalOnMissingBean(MongoConverter.class)
注释,因此添加您自己的 MappingMongoConverter
bean 将导致自动配置放弃支持您的自定义转换器.
That's the right way to do it. The auto-configured MappingMongoConverter
is annotated with @ConditionalOnMissingBean(MongoConverter.class)
, so adding your own MappingMongoConverter
bean will cause the auto-configuration to back off in favour of your custom converter.
一个小更正:您无需调用 mongoConverter.afterPropertiesSet()
.容器会为您调用它.
One minor correction: there's no need for you to call mongoConverter.afterPropertiesSet()
. The container will call that for you.
这篇关于如何在不破坏自动配置的情况下在 Spring-Boot 中自定义 MappingMongoConverter (setMapKeyDotReplacement)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!