【问题标题】:Consider defining a bean of type 'org.flywaydb.core.Flyway'考虑定义一个 'org.flywaydb.core.Flyway' 类型的 bean
【发布时间】:2018-08-03 15:22:30
【问题描述】:

我有一个 Spring Boot 项目,其中 Flyway 与 JPA 一起使用。

<dependency>
  <groupId>org.flywaydb</groupId>
  <artifactId>flyway-core</artifactId>
</dependency>

项目运行良好。由于某些要求,我不得不更改 Flyway 加载的顺序,因此它在 Hibernate 之后加载。我搜索并找到了以下解决方案https://stackoverflow.com/a/44806540/1361888。我按照上面链接中的答案建议创建了MigrationConfiguration。现在,当我运行应用程序时,它会给我以下错误,

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method flywayInitializer in 
com.x2iq.microservice.config.MigrationConfiguration required a bean of type 
'org.flywaydb.core.Flyway' that could not be found.


Action:

Consider defining a bean of type 'org.flywaydb.core.Flyway' in your 
configuration.

现在我对 Spring Boot 还很陌生,所以无法理解这个错误,我在网上找不到与这个错误相关的任何内容。

【问题讨论】:

    标签: java spring hibernate spring-boot flyway


    【解决方案1】:

    @Configuration 类中,您可能希望将 Flyway Bean 公开给容器。像这样的东西应该可以解决问题:

    @Configuration 
    public class MigrationConfiguration {
    
        @Bean
        public Flyway flyway(){
            return new Flyway();
        }
    }
    

    【讨论】:

      【解决方案2】:

      参考以下链接:

      为 flyway bean 提供 Spring 配置。

      https://flywaydb.org/documentation/api/

      我的项目示例:

      <bean id="flyway" class="org.flywaydb.core.Flyway" init-method="migrate">
          <property name="baselineOnMigrate" value="true" />
          <property name="locations" value="classpath:db/migration" />
          <property name="dataSource" ref="dataSource" />
      </bean>
      
      <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
          <property name="driverClassName" value="${db.driver}"/>
          <property name="url" value="${db.url}"/>
          <property name="username" value="${db.username}"/>
          <property name="password" value="${db.password}"/>
      </bean>
      

      【讨论】:

        猜你喜欢
        • 2021-11-25
        • 2019-12-05
        • 2018-06-22
        • 1970-01-01
        • 2020-08-01
        • 2019-02-14
        • 2018-12-21
        • 2019-05-10
        • 2019-09-04
        相关资源
        最近更新 更多