Spring Boot& Mongo:自定义MappingMongoConverter在DotReplacement

时间:2017-09-03 20:39:52

标签: spring spring-boot spring-data spring-data-mongodb spring-mongo

我需要获得dotReplacementKey

我使用MongoDB配置了这样的配置:

@Configuration
public class MongoTemplateConfig {

@Value("${adserver_mongo_connection_string}")
private String databaseConnectionString;

@Bean
public MongoDbFactory mongoDbFactory() throws UnknownHostException {

    MongoClientURI uri = new MongoClientURI(databaseConnectionString.trim());
    return new SimpleMongoDbFactory(uri);
}

@Bean
public MongoTemplate mongoTemplate() throws UnknownHostException {
    return new MongoTemplate(mongoDbFactory());
}

@Bean
public MappingMongoConverter mongoConverter(MongoDbFactory mongoFactory, MongoMappingContext mongoMappingContext) throws Exception {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoFactory);
    MappingMongoConverter mongoConverter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
    mongoConverter.setMapKeyDotReplacement(".");

    return mongoConverter;
}
}

我按如下方式进行upsert: mongoTemplate.bulkOps(...).upsert(...)

但是在运行时通过调试我发现使用了不同的MappingMongoConverter 而不是将其配置为@Bean

顺便说一句,如果是@Inject,那么MappingMongoConverter我从config获得了适当的bean,其中keyDotReplacement ="。" 但看起来像Spring Boot在引擎盖下使用另一个

P.S。我看过这个question没有正确的答案,但尝试了mongoConverter.afterPropertiesSet(),显然它也不起作用

1 个答案:

答案 0 :(得分:2)

这有点荒谬但重点是我们应该将这个自定义mappingMongoConverter传递给MongoTemplate初始化

{{1}}