Difference between MongoFactoryBean and SimpleMongoDbFactory

两盒软妹~` 提交于 2019-11-30 19:36:20

I am surprised this question has still not been answered. And though this might not be the exact answer you are looking for, here is my take on things. I have found that using the second approach, MongoFactoryBean, is the best approach.

Simply because there are more configuration options. For instance, if you want to set an Exception Translator, you can do so easily with MongoFactoryBean.

If I remember correctly, and I may be wrong, the MongoFactoryBean is meant for convenience for MongoDbFactory. Meaning, it adds another layer of abstraction.

All in all, go with the second approach.

I believe that in the first option you can also set the same settings of the second option:

public @Bean MongoDbFactory mongoDbFactory() throws Exception {
    UserCredentials credentials = new UserCredentials(env.getProperty("db.username"), env.getProperty("db.password"));
    return new SimpleMongoDbFactory(new MongoClient(env.getProperty("db.host"), env.getProperty("db.port",Integer.class, 27017)), env.getProperty("db.name"), credentials);
}

public @Bean MongoTemplate mongoTemplate() throws Exception {
    return new MongoTemplate(mongoDbFactory());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!