springspring-bootspring-datamorphia

MongoAutoConfiguration does not create MongoClient bean


I have just read Spring Boot configuration with Morphia?
and I am trying to implement it like this. But I always get

Field mongoClient in MorphiaAutoConfiguration required a bean of type 'com.mongodb.MongoClient' that could not be found.

If I understood the documentation correctly, a MongoClient bean should have been created automatically by the MongoAutoConfiguration. Because I did not deactivate this configuration, it should be loaded due to my @SpringBootApplication annotation on my Main class. Where is my mistake?


Solution

  • Everything does work if I use Morphia 2.2.6. Just in case someone wants to know the details, I will explain what has been bothering me here.

    My old version used 'dev.morphia.morphia:core:1.6.1' and this class

    import com.mongodb.MongoClient; // Bad example; this code will not run!
    import dev.morphia.Datastore;
    import dev.morphia.Morphia;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MorphiaAutoConfiguration {
    
      @Autowired
      private MongoClient mongoClient; // will not be created from MongoAutoConfiguration !!!!!
    
      @Bean
      public Datastore datastore() {
        return new Morphia().createDatastore(mongoClient, "mongodb");
      }
    }
    

    Now i'm using 'dev.morphia.morphia:morphia-core:2.2.6' and

    import com.mongodb.client.MongoClient;
    import dev.morphia.Datastore;
    import dev.morphia.Morphia;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MorphiaAutoConfiguration {
    
      @Autowired
      private MongoClient mongoClient; // created from MongoAutoConfiguration
    
      @Bean
      public Datastore datastore() {
        return Morphia.createDatastore(mongoClient, "mongodb");
      }
    }
    

    Note that the MongoClient comes from another package. So my services has provided a com.mongodb.client.MongoClient Bean but no com.mongodb.MongoClient Bean, hence my confusion.