我有一个spring boot应用程序,我想为其添加liquibase配置更改日志。
我创建了一个LiquibaseConfig类来配置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。我怎样才能做到这一点?
MysqlDataSource dataSource;
这是将liquibase集成到Spring Boot中的简单步骤
添加liquibase依赖
runtime "org.liquibase:liquibase-core"
<dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> <scope>runtime</scope> </dependency>
在以下位置添加liquibase changelog文件路径 application.yml
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. ,所以你应该有一个文件名 db-changelog.xml 中 src/main/resources/liquibase/
liquibase.change-log.
/liquibase/db-changelog.xml.
db-changelog.xml
src/main/resources/liquibase/
将更改集添加到文件上,并在启动Spring-Boot应用程序(spring-boot:run)时加载您的更改集。
spring-boot:run
这将使用dataSource您的应用程序使用的默认值。
dataSource
更多信息: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
spring: liquibase: change-log: #path