在spring boot中为liquibase配置dataSource

Muh*_*ahy 8 liquibase applicationcontext spring-boot

我有一个spring boot应用程序,我想为它添加liquibase配置更改日志.

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

@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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

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)}
Run Code Online (Sandbox Code Playgroud)

现在我明白这意味着应用程序无法自动装配,MysqlDataSource dataSource;但我需要将数据源传递给liquibase bean.我怎样才能做到这一点?

Ama*_*har 14

这是将liquibase整合到弹簧靴中的简单步骤

步骤1

添加liquibase依赖项

摇篮

runtime "org.liquibase:liquibase-core"
Run Code Online (Sandbox Code Playgroud)

Maven的

<dependency>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-core</artifactId>
    <scope>runtime</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

第2步

添加liquibase changelog文件路径 application.yml

liquibase:
  enabled: true #this is optional as enabled by default
  change-log: classpath:/liquibase/db-changelog.xml
Run Code Online (Sandbox Code Playgroud)

注意属性liquibase.change-log.我指的是路径,/liquibase/db-changelog.xml.所以你应该db-changelog.xml在里面 有一个文件名src/main/resources/liquibase/

第3步

在文件中添加更改集,并在启动Spring-Boot应用程序时(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
Run Code Online (Sandbox Code Playgroud)

  • 你基本上是正确的,在最简单的情况下,这应该是开箱即用的自动配置(和较少的样板代码).更改日志应该应用于主数据源(我假设他只有一个),但是有多个源,你可能想要自己实例化自定义初始化的SpringLiquibase bean ... (3认同)
  • 请注意,使用Spring启动2时,属性不再是"liquibase.change-log",而是"spring.liquibase.change-log" (2认同)