我如何MappingMongoConverter在不更改任何由spring- data自动配置的mongo-stuff的情况下自定义Spring-Boot-Application(1.3.2.RELEASE)中的?
MappingMongoConverter
我当前的解决方案是:
@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?
这是正确的方法。自动配置MappingMongoConverter的带有注释@ConditionalOnMissingBean(MongoConverter.class),因此添加您自己的MappingMongoConverterbean将导致自动配置退而转而支持您的自定义转换器。
@ConditionalOnMissingBean(MongoConverter.class)
一项较小的更正:您无需致电mongoConverter.afterPropertiesSet()。容器会为您调用。
mongoConverter.afterPropertiesSet()