如何在 Spring-Boot 中自定义 MappingMongoConverter (setMapKeyDotReplacement) 而不会破坏自动配置? [英] How to customize MappingMongoConverter (setMapKeyDotReplacement) in Spring-Boot without breaking the auto-configuration?

查看:29
本文介绍了如何在 Spring-Boot 中自定义 MappingMongoConverter (setMapKeyDotReplacement) 而不会破坏自动配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何自定义 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) 而不会破坏自动配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆