在 spring 引导中为 liquibase 配置数据源

configure dataSource for liquibase in spring boot

我有一个 spring 引导应用程序,我想为其添加 liquibase 配置更改日志。

我创建了一个 LiquibaseConfig class 用于配置 liquibase:

@Configuration
public class LiquibaseConfiguration {

    @Value("${com.foo.bar.liquibase.changelog}")
    private String changelog;

    @Autowired
    MysqlDataSource dataSource;

    @Bean
    public SpringLiquibase liquibase()  {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog(changelog);
        return liquibase;
    }

}

并且我已经在属性文件中配置了数据源信息:

spring.datasource.url=jdbc:mysql://localhost:3306/dms
spring.datasource.username=root
spring.datasource.password=test
spring.datasource.testWhileIdle = true
spring.jpa.show-sql = true

#liquibase
com.foo.bar.liquibase.changelog=classpath:/db/changelog/db.changelog.xml

当我 运行 我的应用程序收到此错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'liquibaseConfiguration': Unsatisfied dependency expressed through field 'dataSource': No qualifying bean of type [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] found for dependency [com.mysql.jdbc.jdbc2.optional.MysqlDataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mysql.jdbc.jdbc2.optional.MysqlDataSource] found for dependency [com.mysql.jdbc.jdbc2.optional.MysqlDataSource]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

现在我明白这意味着应用程序无法自动装配 MysqlDataSource dataSource; 但我需要将数据源传递给 liquibase bean。我该怎么做?

这是在 spring boot

中集成 liquibase 的简单步骤

第 1 步

添加 liquibase 依赖项

Gradle

runtime "org.liquibase:liquibase-core"

行家

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <scope>runtime</scope>
</dependency>

第 2 步

application.yml

中添加 liquibase 更新日志文件路径
liquibase:
  enabled: true #this is optional as enabled by default
  change-log: classpath:/liquibase/db-changelog.xml

注意 属性 liquibase.change-log. 我指的路径是 /liquibase/db-changelog.xml. 所以你应该在 src/main/resources/liquibase/

中有一个文件名 db-changelog.xml

第 3 步

在文件中添加您的变更集,当 Spring-启动应用程序时 (spring-boot:run) 您的变更集将被加载。

这将使用您的应用使用的默认 dataSource

更多信息:http://docs.spring.io/spring-boot/docs/1.4.3.RELEASE/reference/htmlsingle/#howto-execute-liquibase-database-migrations-on-startup

更新

对于 Spring Boot 2.0 正如@veben 在评论中指出的那样

spring:
    liquibase:
        change-log: #path

我的应用程序有一个类似的构造,您是否尝试过注入更通用的 javax.sql.DataSource

我的猜测是 Spring 为此 bean 使用了非特定类型,除此之外,如果您的应用程序可以配置为使用完全不同的类型,您甚至不应该使用特定于数据库的类型源,即只需更改配置文件中的数据源 URL。

与其自动装配数据源,不如在 Liquibase 配置对象中创建一个方法来创建数据源。

private DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUsername("user");
    dataSource.setPassword("pswd");
    dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/db");
    dataSource.setSchema("dbName");
    return dataSource;
}

然后在你的bean生成方法中

liquibase.setDataSource(dataSource());

这让您有机会即时指定数据库参数,我仍然在 applications.properties 文件中有更改日志位置以及 liquibase 的启用。我试过了,对我有用。