title: MongoTemplate Custom Config using Spring boot
body: <p>I was looking to change WriteResultChecking property of mongoTemplate whilst working on Spring boot app (2.0.5). I found out a way via extending AbstractMongoConfiguration as below.</p>
  
  <p>I got the same working, however i found this approach a bit risky.
  Saying this because this approach forced me to write a implementation for</p>
  
  <pre><code>public MongoClient mongoClient() {
      return new MongoClient(host, port);
  }
  </code></pre>
  
  <p>Now MongoClient is the central class to maintain connections with MongoDB and if i am forced to write implementation for the same, then i may be possibly missing out on optimizations that spring framework does.</p>
  
  <p>Can someone please suggest any other optimal way of overriding some properties/behaviours without having to tinker too much ?</p>
  
  <pre><code>@Configuration
  public class MyMongoConfigs extends AbstractMongoConfiguration {
  
  
      @Value("${spring.data.mongodb.database}")
      private String databaseName;
  
      @Value("${spring.data.mongodb.host}")
      private String host;
  
      @Value("${spring.data.mongodb.port}")
      private int port;
  
  
      @Override
      public MongoClient mongoClient() {
          return new MongoClient(host, port);
  
      }
  
      @Override
      protected String getDatabaseName() {
          return databaseName;
      }
  
      @Bean
      public MongoTemplate mongoTemplate() throws Exception {
          MongoTemplate myTemp = new MongoTemplate(mongoDbFactory(), mappingMongoConverter());
          **myTemp.setWriteResultChecking(WriteResultChecking.EXCEPTION);**
          return myTemp;
      }
  </code></pre>