【问题标题】:An attempt was made to call a method that does not exist com.github.cloudyrock.mongock.SpringMongockBuilderBase.lambda$getMongoTemplateProxySupplier尝试调用不存在的方法 com.github.cloudyrock.mongock.SpringMongockBuilderBase.lambda$getMongoTemplateProxySupplier
【发布时间】:2020-07-15 06:29:41
【问题描述】:

使用 Mongock https://github.com/cloudyrock/mongock 进行数据迁移 spring boot 2.3.1.RELEASE,之前使用的是 Mongobee 但由于这个错误org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating bean with name 'mongoTemplate' 以及大家转用Mongock的建议An attempt was made to call a method that does not exist, mongo-java-driver , mongobee

现在我正在使用 Mongock 进行数据迁移,但总是报错

An attempt was made to call a method that does not exist. The attempt was made from the following location:

    com.github.cloudyrock.mongock.SpringMongockBuilderBase.lambda$getMongoTemplateProxySupplier$0(SpringMongockBuilderBase.java:138)

The following method did not exist:

    'org.springframework.data.mongodb.MongoDbFactory org.springframework.data.mongodb.core.MongoTemplate.getMongoDbFactory()'

The method's class, org.springframework.data.mongodb.core.MongoTemplate, is available from the following locations:

    jar:file:/Users/macbook/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-mongodb/3.0.1.RELEASE/24961d107813268bb9cd3daa8d4a9389060faea7/spring-data-mongodb-3.0.1.RELEASE.jar!/org/springframework/data/mongodb/core/MongoTemplate.class

The class hierarchy was loaded from the following locations:

    org.springframework.data.mongodb.core.MongoTemplate: file:/Users/macbook/.gradle/caches/modules-2/files-2.1/org.springframework.data/spring-data-mongodb/3.0.1.RELEASE/24961d107813268bb9cd3daa8d4a9389060faea7/spring-data-mongodb-3.0.1.RELEASE.jar


Action:

Correct the classpath of your application so that it contains a single, compatible version of org.springframework.data.mongodb.core.MongoTemplate

这是错误是由于依赖我知道但不知道如何解决

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-data-rest'
    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 "org.mongodb:mongo-java-driver:3.12.6"
    compile("com.github.cloudyrock.mongock:mongock-spring:3.3.2") {
        exclude group: 'org.mongodb', module: 'mongo-java-driver'
    }
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

test {
    useJUnitPlatform()
}

【问题讨论】:

    标签: spring spring-boot spring-data-mongodb mongo-java-driver mongock


    【解决方案1】:

    问题出在 mongock:mongock,更新到最新的 Beta 版本,通过添加工作解决了问题。

    移除所有的 mongock 依赖并添加下面的依赖

    compile "com.github.cloudyrock.mongock:mongock-bom:4.1.2.BETA"
        compile 'com.github.cloudyrock.mongock:mongock-spring-v5:4.1.2.BETA'
        compile 'com.github.cloudyrock.mongock:mongodb-springdata-v3-driver:4.1.2.BETA'
    

    现在 Bean 改变如下

      @Bean
        public MongockSpring5.MongockInitializingBeanRunner mongockInitializingBeanRunner(ApplicationContext springContext, MongoTemplate mongoTemplate) {
            boolean migrationsEnabled = Boolean.parseBoolean(environment.getProperty("app.db.migrations.enabled"));
            return MongockSpring5.builder()
                    .setDriver(new SpringDataMongo3Driver(mongoTemplate,4,4,3))
                    .addChangeLogsScanPackages(List.of("fete.bird.fetebirdproduct.migration"))
                    .setSpringContext(springContext)
                    .setEnabled(migrationsEnabled)
                    .buildInitializingBeanRunner();
        }
    

    参考

    https://github.com/cloudyrock/mongock/issues/195

    【讨论】:

      【解决方案2】:

      除了 San 的回答,这个 issue 可能更有用一点。(请注意您应该将 Mongock 的版本更新到 4.1.2.BETA)

      您可以看到您不需要自己构建 bean,通过使用 @EnableMongock 注释您的 Spring 应用程序,它会为您完成工作。

      这个sample project也值得一看

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-28
        • 2019-02-05
        • 2014-03-26
        • 1970-01-01
        • 1970-01-01
        • 2017-10-01
        • 2020-02-28
        • 2012-07-03
        相关资源
        最近更新 更多