简体   繁体   English

创建 MongoTemplate bean 时出现 UnsatisfiedDependencyException

[英]UnsatisfiedDependencyException when creating MongoTemplate bean

Using the Mongobee for Migration with spring mongo使用 Mongobee 与 spring mongo 进行迁移

 plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}

group = 'fete.bird'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '14'
compileJava {
    options.compilerArgs += ["--enable-preview"]
}
repositories {
    mavenCentral()
}
ext {
    set('springCloudVersion', "Hoxton.SR6")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
    implementation 'org.springdoc:springdoc-openapi-ui:1.4.3'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    implementation 'org.springframework.cloud:spring-cloud-starter-config'
    implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

    compile 'com.github.mongobee:mongobee:0.13'
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

Doing the constructor dependency for MongoTemplate causing the error org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'mongoTemplate'为 MongoTemplate 执行构造函数依赖导致错误org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'mongoTemplate'

Here is the configuration code下面是配置代码

@Configuration
public class ProductMigration {
    private final Environment environment;
    private MongoTemplate mongoTemplate;
    private static final Logger logger = LoggerFactory.getLogger(FeteBirdProductApplication.class);

    public ProductMigration(Environment environment, MongoTemplate mongoTemplate) {
        this.environment = environment;
        this.mongoTemplate = mongoTemplate;
    }

    @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(this.mongoTemplate);
        logger.info("Product migration completed...");
        return runner;
    }
}

The issue is caused by constructor dependency for MongoTemplate, but don't know how to solve the issue,该问题是由 MongoTemplate 的构造函数依赖引起的,但不知道如何解决该问题,

I tried @Autowired, didn't work same issue.我试过@Autowired,没有解决同样的问题。

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryDependentConfiguration.class]: Unsatisfied dependency expressed through method 'mongoTemplate' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'mongoDatabaseFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoSuchFieldError: UNSPECIFIED
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:538) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1304) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1224) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    ... 25 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Unsatisfied dependency expressed through method 'mongoDatabaseFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongo' defined in class path resource [org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mongodb.client.MongoClient]: Factory method 'mongo' threw exception; nested exception is java.lang.NoSuchFieldError: UNSPECIFIED
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:797) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:538) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1304) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1224) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:884) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:788) ~[spring-beans-5.2.7.RELEASE.jar:5.2.7.RELEASE]
    ... 38 common frames omitted

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. 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.这导致驱动程序版本 4 中的一些代码使用版本 3 中的代码和NoSuchFieldError结果。

You can avoid the problem by excluding the Mongo Driver from the Mongobee dependency:您可以通过从 Mongobee 依赖项中排除 Mongo Driver 来避免此问题:

compile('com.github.mongobee:mongobee:0.13') {
    exclude group: 'org.mongodb'
}

It seems you don't have the bean created for "MongoTemplate"看来您没有为“MongoTemplate”创建的 bean

Create a bean for it and use that to setMongoTemplate for Mongobee.为它创建一个 bean 并使用它为 Mongobee 设置MongoTemplate。

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现在,您不必使用构造函数依赖项,以后可以在任何想要使用 MongoTemplate 的地方使用

 @Autowired  
 MongoTemplate mongoTemplate;

Hope, this will solve your problem !!希望,这将解决您的问题!

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 . 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. Spring 数据 MongoDB 需要版本 4 的 Mongo Java 库,而 mongobee 需要版本 3。这两个版本互不兼容,不能在一个应用程序内同时使用。

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.种种迹象表明,mongobee 已被其创建者抛弃,最后一次提交是在 2018 年 3 月,此后创建者没有对问题作出回应。 Therefore, don't expect an updated version of that library to be released with support for the new version of the Mongo Java drivers.因此,不要期望发布该库的更新版本以支持新版本的 Mongo Java 驱动程序。

Due to this project abandonment, several successor libraries were forked from mongobee.由于这个项目的放弃,几个后继库是从 mongobee 分叉出来的。 From what I have been able to determine, Mongock is the only remaining actively maintained successor library.据我所知, Mongock是唯一剩下的积极维护的后继库。 It has support for both version 3 and version 4 of the Mongo Java libraries.它支持 Mongo Java 库的版本 3 和版本 4。

Mongock is a significant evolution of Mongobee, with built-in support for migrating from mongobee . Mongock 是 Mongobee 的重大演变,内置支持从 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.除了对 Mongo Java 驱动程序版本 4 的内置支持外,它还具有与 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.因此,如果您希望使用最新版本的 Spring Data MongoDB 与 mongobee 兼容的迁移库,Mongock 将是最简单、最直接的方法。

dependencies {
    [...]

    compile 'com.github.cloudyrock.mongock:mongock-api:4.1.14'
}

Here it is,这里是,

@Bean
public MongoClient mongo() {
    return new MongoClient("localhost");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UnsatisfiedDependencyException:创建bean时出错 - UnsatisfiedDependencyException: error creating bean UnsatisfiedDependencyException - 创建具有名称的 bean 时出错 - UnsatisfiedDependencyException - Error creating bean with name UnsatisfiedDependencyException:创建bean时出错(通过BeanNotOfRequiredTypeException) - UnsatisfiedDependencyException: Error creating bean(by BeanNotOfRequiredTypeException) UnsatisfiedDependencyException:使用名称创建 bean 时出错 - UnsatisfiedDependencyException: Error creating bean with name 创建名为 'mongoTemplate' 的 bean 时出错 - Error creating bean with name 'mongoTemplate' Jenkins 构建 UnsatisfiedDependencyException:创建带有名称的 bean 时出错 - Jenkin build UnsatisfiedDependencyException: Error creating bean with name UnsatisfiedDependencyException:创建名称为“registrationController”的 bean 时出错 - UnsatisfiedDependencyException: Error creating bean with name 'registrationController' 错误:UnsatisfiedDependencyException:创建名为“entityManagerFactory”的 bean 时出错 - Error: UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' REST API 中创建bean名时出现UnsatisfiedDependencyException错误 - UnsatisfiedDependencyException error in creating bean name in REST API UnsatisfiedDependencyException:创建名为“empController”的bean时出错 - UnsatisfiedDependencyException: Error creating bean with name “empController”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM