UnsatisfiedDependencyException when creating MongoTemplate bean

后端 未结 4 1633
南方客
南方客 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: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 !!

提交回复
热议问题