UnsatisfiedDependencyException when creating MongoTemplate bean

后端 未结 4 1634
南方客
南方客 2021-01-25 16:48

Using the Mongobee for Migration with spring mongo

 plugins {
    id \'org.springframework.boot\' version \'2.3.1.RELEASE\'
    id \'io.spring.dependency-manageme         


        
相关标签:
4条回答
  • 2021-01-25 17:13

    Here it is,

    @Bean
    public MongoClient mongo() {
        return new MongoClient("localhost");
    }
    
    0 讨论(0)
  • 2021-01-25 17:14

    The version of Spring Data MongoDB included in Spring Boot 2.3 uses version 4 of Mongo's Driver, but Mongobee depends on version 3. The two versions have different Maven coordinates (group ID and artifact ID) so you end up with both of them on the classpath. This leads to some code from version 4 of the driver using code from version 3 and a NoSuchFieldError results.

    You can avoid the problem by excluding the Mongo Driver from the Mongobee dependency:

    compile('com.github.mongobee:mongobee:0.13') {
        exclude group: 'org.mongodb'
    }
    
    0 讨论(0)
  • 2021-01-25 17:15

    You are running across an incompatibility between the MongoDB Java driver required by the newer versions of Spring Data MongoDB, and the one used by mongobee. Spring Data MongoDB requires version 4 of the Mongo Java libraries, whereas mongobee requires version 3. The two versions are incompatible with each other, and cannot be used concurrently within an application.

    By all indications, mongobee has been abandoned by its creators, with the last commit being made in March, 2018, and no responses to issues by the creators since then. Therefore, don't expect an updated version of that library to be released with support for the new version of the Mongo Java drivers.

    Due to this project abandonment, several successor libraries were forked from mongobee. From what I have been able to determine, Mongock is the only remaining actively maintained successor library. It has support for both version 3 and version 4 of the Mongo Java libraries.

    Mongock is a significant evolution of Mongobee, with built-in support for migrating from mongobee. In addition to its built-in support for version 4 of the Mongo Java driver, it has optional built-in integration with Spring & Spring Boot.

    Therefore, if you wish to use a recent version of Spring Data MongoDB with a mongobee-compatible migration library, Mongock is going to the be easiest, most straightforward approach.

    dependencies {
        [...]
    
        compile 'com.github.cloudyrock.mongock:mongock-api:4.1.14'
    }
    
    0 讨论(0)
  • 2021-01-25 17:16

    It seems you don't have the bean created for "MongoTemplate"

    Create a bean for it and use that to setMongoTemplate for Mongobee.

    Below is the example to do so

    @Bean
    public MongoClient mongo() {
        return new MongoClient("localhost");
    }
    
    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
       return new MongoTemplate(mongo(), "test");
    }
    

    then use this

    @Bean
    public Mongobee mongobee(){
        logger.info("Starting product migration ...");
        String mongoUri = environment.getProperty("spring.data.mongodb.uri");
        boolean migrationsEnabled = Boolean.parseBoolean(environment.getProperty("app.db.migrations.enabled"));
        Mongobee runner = new Mongobee(mongoUri);
        runner.setEnabled(migrationsEnabled);
        runner.setChangeLogsScanPackage("fete.bird.fetebirdproduct.migration");
        runner.setChangelogCollectionName("migrations");
        runner.setLockCollectionName("migrations_lock");
        runner.setMongoTemplate(mongoTemplate());
        logger.info("Product migration completed...");
        return runner;
    }
    

    Now, you don't have to use constructor dependency and later wherever you want MongoTemplate you can use

     @Autowired  
     MongoTemplate mongoTemplate;
    

    Hope, this will solve your problem !!

    0 讨论(0)
提交回复
热议问题