flyway with spring boot and gradle--org.flywaydb.core.Flyway: 方法 <init>()V not found

Posted

技术标签:

【中文标题】flyway with spring boot and gradle--org.flywaydb.core.Flyway: 方法 <init>()V not found【英文标题】:flyway with spring boot and gradle--org.flywaydb.core.Flyway: method <init>()V not found 【发布时间】:2020-01-26 22:02:07 【问题描述】:

我有一个带有 gradle 并使用 postgres db 的 spring boot 项目。我想通过使用 flyway 进行数据库迁移进行测试。我面临以下错误。我的 build.gradle 和 application.properties 也在下面给出。请帮我解决这个错误。我相信这与设置有关,但与实际代码无关。

错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flyway' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.NoSuchMethodError: org.flywaydb.core.Flyway: method <init>()V not found
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1177) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1072) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:297) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1082) ~[spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:857) ~[spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:123) ~[spring-boot-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:666) [spring-boot-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:353) [spring-boot-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:300) [spring-boot-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1082) [spring-boot-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1071) [spring-boot-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at com.ford.mach1mldb.AccessingDataRestApplication.main(AccessingDataRestApplication.java:10) [main/:na]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.flywaydb.core.Flyway]: Factory method 'flyway' threw exception; nested exception is java.lang.NoSuchMethodError: org.flywaydb.core.Flyway: method <init>()V not found
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    ... 20 common frames omitted
Caused by: java.lang.NoSuchMethodError: org.flywaydb.core.Flyway: method <init>()V not found
    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$SpringBootFlyway.<init>(FlywayAutoConfiguration.java:184) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$SpringBootFlyway.<init>(FlywayAutoConfiguration.java:184) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration.flyway(FlywayAutoConfiguration.java:130) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration$$EnhancerBySpringCGLIB$$1b5373a9.CGLIB$flyway$0(<generated>) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration$$EnhancerBySpringCGLIB$$1b5373a9$$FastClassBySpringCGLIB$$b74dfec1.invoke(<generated>) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:358) ~[spring-context-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    at org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration$$EnhancerBySpringCGLIB$$1b5373a9.flyway(<generated>) ~[spring-boot-autoconfigure-1.5.22.RELEASE.jar:1.5.22.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_221]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_221]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_221]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_221]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.3.25.RELEASE.jar:4.3.25.RELEASE]
    ... 21 common frames omitted

Build.gradle:

buildscript 
    ext 
        springBootVersion = '1.5.22.RELEASE'
    
    repositories 
        mavenCentral()
        jcenter()
        maven 
         url "https://plugins.gradle.org/m2/"
      
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion")

    


apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

repositories 
    mavenCentral()

dependencies 

    compile( 'org.springframework.boot:spring-boot-starter-data-rest' )
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile files('./lib/postgresql-42.2.8.jar') 
    compile group: 'org.flywaydb', name: 'flyway-core', version: '6.0.1'
    compile group: 'javax.validation', name: 'validation-api', version: '2.0.0.Final'   
    compile group: 'org.springframework.ws', name: 'spring-ws-support'
    compile('org.springframework:spring-context-support')
    compile "org.apache.httpcomponents:httpmime:4.2.3"
    compile "org.apache.httpcomponents:httpclient:4.5.2"
    compile "org.apache.httpcomponents:httpcore:4.4.5"
    compile group: 'com.squareup.okhttp', name: 'okhttp', version: '2.7.5' 


dependencyManagement 
    imports 
        mavenBom "io.pivotal.spring.cloud:spring-cloud-services-dependencies:1.4.1.RELEASE"
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.SR5"
    


Application.properties:

spring.jpa.show-sql=true

#postgress
spring.datasource.url=jdbc:postgresql://xxx/xxx
spring.datasource.username=xxx
spring.datasource.password=
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect


flyway.baseline-on-migrate=true 

【问题讨论】:

【参考方案1】:

是的,我还将我的 flyway 版本从 5.2.4 升级到 7.0.0 并遇到同样的错误。

为了解决这个问题,我将 flyway 版本降级到 6.5.0,它现在可以工作了!

【讨论】:

【参考方案2】:

在 Gradle 中尝试其他版本的 flyway(旧)或删除版本(它对我使用 maven 有帮助)。

【讨论】:

以上是关于flyway with spring boot and gradle--org.flywaydb.core.Flyway: 方法 <init>()V not found的主要内容,如果未能解决你的问题,请参考以下文章

Spring-Boot 不适用于 Flyway

Flyway 和 Spring Boot 集成

使用 Spring Boot 进行 Flyway 修复

无法在 Spring Boot 中将 Flyway 迁移与 postgresQL 连接起来

Flyway 在 Spring Boot 上不工作时如何调试?

将 Flyway 设置为在 Spring Boot 中使用不同的环境