7

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?

mingzhao.pro
  • 709
  • 1
  • 6
  • 20
  • is it pure spring or spring-boot? I would not worry about using just Mongo mongo() - you will not call this method in your code anyway - it will be used by configuration beans, and then you are probably going to use repositories or MongoTemplate. – hi_my_name_is Jul 21 '15 at 12:09
  • @freakman it's pure spring. Spring-boot does too much under the table and I can understand well. You are right that I will just use MongoTemplate, I just wonder if the **Offical Reference** gives some wrong information, how could we master it well. :( – mingzhao.pro Jul 21 '15 at 13:19

2 Answers2

5

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.

Community
  • 1
  • 1
thomas.g
  • 3,894
  • 3
  • 29
  • 36
  • In fact I dont care about the usage of SimpleMongoDbFactory, but according to SpringFramework, this is the only way to **get rid of MongoDB api** and gain a **pure SpringFramework** environment. To use SimpleMongoDbFactory could avoid some license problem if the final product turns to be commercial, right ? :) – mingzhao.pro Oct 05 '15 at 13:28
  • What do you mean by get rid of MongoDB api ? It seems that SimpleMongoDbFactory still needs a com.mongodb.MongoClient which is part of MongoDB api anyway ? As the doc says it's just a factory to create DB instances from a Mongo instance. Am I missing something ? – thomas.g Oct 05 '15 at 21:55
  • In fact all the org.springframework.data.mongodb.core is a Spring layer above the MongoDB api. – thomas.g Oct 05 '15 at 21:59
  • Not sure if you can get into licensing problems unless you modify Mongo's source, see http://programmers.stackexchange.com/questions/226111/can-i-use-mongodb-for-a-commercial-web-based-service – thomas.g Oct 05 '15 at 22:04
  • *get rid of mongo api* I mean donnot use it explicitely in my code and this is the idea that springframework gave me. According to the current information we know that it is not possible. So your solution is what I use. thank you again. and the license problem I think there will be no problem since i dont touch the mongodb driver. – mingzhao.pro Oct 06 '15 at 05:41
  • Ok. At best you can wire your beans inside your configuration and only expose a MongoTemplate or work with a MongoRepository so the MongoDB api shouldn't surface to much in your code. – thomas.g Oct 06 '15 at 06:00
3

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")

Nayan
  • 1,521
  • 2
  • 13
  • 27