I am setting up a MongoDB Spring MVC application and trying to use Service, DAO pattern.
I read the Spring-Data-MongoDB refernce here, but I am not understanding what is the differnce between MongoFactoryBean and SimpleMongoDbFactory.
What would be a better way, and why, to create a MongoTemplate bean.
@Configuration
public class SpringMongoConfig {
public @Bean
MongoDbFactory mongoDbFactory() throws Exception {
return new SimpleMongoDbFactory(new MongoClient(), "yourdb");
}
public @Bean
MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory());
return mongoTemplate;
}
}
OR.
@Bean
public MongoFactoryBean mongo() {
MongoFactoryBean mongo = new MongoFactoryBean();
mongo.setHost(env.getProperty("db.host"));
mongo.setPort(env.getProperty("db.port",Integer.class,27017));
return mongo;
}
@Bean
public MongoTemplate mongoTemplate() throws Exception{
return new MongoTemplate(mongo().getObject(),env.getProperty("db.name"));
}
When do i use MongoFactoryBean and when do i use MongoDbFactory? Do they have different use cases?
Also what would be the best way to bootstrap MongoDB into Spring MVC, in such a way that its highly scaleable and configurable, and also provides provision to plug in any other RDBMS( for same or different functionalty). (Two different DAO impls for various DB types perhaps?)