title: Spring Mongodb: How to configurer mongoDB with MongoClientFactoryBean
body: <p>When configuring MongoDB in Spring, the reference sais:</p>
  
  <p>register MongoDB like this:    </p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
    /*
     * Use the standard Mongo driver API to create a com.mongodb.Mongo instance.
     */
     public @Bean Mongo mongo() throws UnknownHostException {
         return new Mongo("localhost");
     }
  }    
  </code></pre>
  
  <blockquote>
    <p>pollutes the code with the UnknownHostException checked exception. The use of the checked exception is not desirable as Java based bean metadata uses methods as a means to set object dependencies, making the calling code cluttered.</p>
  </blockquote>
  
  <p>so Spring proposes   </p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
  /*
   * Factory bean that creates the com.mongodb.Mongo instance
   */
   public @Bean MongoFactoryBean mongo() {
        MongoFactoryBean mongo = new MongoFactoryBean();
        mongo.setHost("localhost");
        return mongo;
   }
  }
  </code></pre>
  
  <p>But unfortunately since Spring-Data-MongoDB 1.7 <strong>MongoFactoryBean</strong> has been deprecated and replaced by <strong>MongoClientFactoryBean</strong>.</p>
  
  <p>So   </p>
  
  <pre><code>@Bean
  public MongoClientFactoryBean mongoClientFactoryBean() {
      MongoClientFactoryBean factoryBean = new MongoClientFactoryBean();
      factoryBean.setHost("localhost");
      return factoryBean;
  }
  </code></pre>
  
  <p>Then it's time to configure MongoDbFactory which has only one implementation <strong>SimpleMongoDbFactory</strong>. The SimpleMongoDbFactory has only two initializer not deprecated one of which is <strong>SimpleMongoDbFactory(MongoClient, DataBase)</strong>.
  But <strong>MongoClientFactoryBean</strong> can only return type of <strong>Mongo</strong> instead of <strong>MongoClient</strong>.</p>
  
  <p>So, am I missing something to make this pure Spring configuration work?</p>