Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Mongodb: How to configurer mongoDB with MongoClientFactoryBean

When configuring MongoDB in Spring, the reference sais:

register MongoDB like this:

@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");
   }
}    

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.

so Spring proposes

@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;
 }
}

But unfortunately since Spring-Data-MongoDB 1.7 MongoFactoryBean has been deprecated and replaced by 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 SimpleMongoDbFactory. The SimpleMongoDbFactory has only two initializer not deprecated one of which is SimpleMongoDbFactory(MongoClient, DataBase). But MongoClientFactoryBean can only return type of Mongo instead of MongoClient.

So, am I missing something to make this pure Spring configuration work?

like image 639
mingzhao.pro Avatar asked Jul 21 '15 11:07

mingzhao.pro


People also ask

Can I use spring data JPA with MongoDB?

Yes, DataNucleus JPA allows it, as well as to many other databases.

What can I use instead of MongoDbFactory?

Interface MongoDbFactory. Deprecated. since 3.0, use MongoDatabaseFactory instead.

Can we connect MongoDB with spring boot?

It's Easy to Connect MongoDB Atlas with Spring Boot Through this article, we demonstrated that MongoDB Spring Boot integration is easy using: Starter data MongoDB artifactid (the dependency we added while creating the Spring Initializr project) in pom. xml. A property on application.


2 Answers

Yes it returns a Mongo :-(

But as MongoClient extends Mongo that'll be ok anyway, just @Autowire the bean as a Mongo

@Autowired
private Mongo mongo;

Then use it

MongoOperations mongoOps = new MongoTemplate(mongo, "databaseName");

Do you really need the SimpleMongoDbFactory ? See this post.

like image 144
thomas.g Avatar answered Nov 15 '22 09:11

thomas.g


In my case, I'm using the following code to create MongoTemplate. I'm using MongoRespository. As it only needs MongoTemplate I need to create the MongoTemplate bean only.

@Bean
public MongoTemplate mongoTemplate() throws Exception {
    MongoClient mongoClient = new MongoClient("localhost");
    MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, "kyc_reader_test");
    return new MongoTemplate(mongoDbFactory);
}

In my configuration file, I've added

@EnableMongoRepositories(basePackages = "mongo.repository.package.name")

like image 23
Nayan Avatar answered Nov 15 '22 08:11

Nayan