When configuring MongoDB in Spring, the sais link is:
register MongoDB as follows:
@Configuration public class AppConfig { public @Bean Mongo mongo() throws UnknownHostException { return new Mongo("localhost"); } }
pollutes the code with an UnknownHostException exception. Using a checked exception is undesirable because Java bean-based metadata uses methods as a means to set object dependencies, which makes the call code cluttered.
therefore Spring offers
@Configuration public class AppConfig { public @Bean MongoFactoryBean mongo() { MongoFactoryBean mongo = new MongoFactoryBean(); mongo.setHost("localhost"); return mongo; } }
But unfortunately, since Spring -Data-MongoDB 1.7 MongoFactoryBean is deprecated and replaced with MongoClientFactoryBean .
So
@Bean public MongoClientFactoryBean mongoClientFactoryBean() { MongoClientFactoryBean factoryBean = new MongoClientFactoryBean(); factoryBean.setHost("localhost"); return factoryBean; }
Then it's time to configure MongoDbFactory, which has only one implementation of SimpleMongoDbFactory . SimpleMongoDbFactory has only two initializers that are not deprecated, one of which is SimpleMongoDbFactory (MongoClient, DataBase) . But MongoClientFactoryBean can return Mongo type instead of MongoClient .
So, did I miss something to make this clean Spring configuration?
source share