在Spring Boot中为liquibase配置dataSource [英] configure dataSource for liquibase in spring boot

查看:755
本文介绍了在Spring Boot中为liquibase配置dataSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a spring boot application and I want to add liquibase configuration change log for it.

我创建了一个LiquibaseConfig类来配置liquibase:

I have created a LiquibaseConfig class for configuring 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;
    }

}

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

and I have configured the datasource information in properties file:

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

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

when I run my application I receive this error:

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.我该怎么办?

Now I understood that this means the application cannot autowire the MysqlDataSource dataSource; but I need to pass the data source to liquibase bean. How can I do that?

推荐答案

这是将liquibase集成到Spring Boot中的简单步骤

Here's a simple step to integrate liquibase in spring boot

添加liquibase依赖项

Add liquibase dependency

runtime "org.liquibase:liquibase-core"

Maven

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

STEP 2

application.yml

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/

Notice property liquibase.change-log. I'm referring path as /liquibase/db-changelog.xml. so you should have a file name db-changelog.xml inside src/main/resources/liquibase/

将更改集添加到文件上,并在启动Spring-Boot应用程序(spring-boot:run)时加载您的更改集.

Add your changesets on the file and when Spring-Boot application is started (spring-boot:run) your changeset will be loaded.

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

This will use default dataSource that your app uses.

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

对于@veben在注释使用中指出的Spring Boot 2.0

For Spring Boot 2.0 as @veben pointed out in comment use

spring:
    liquibase:
        change-log: #path

这篇关于在Spring Boot中为liquibase配置dataSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆