首页 文章

使用Liquibase重载属性的Spring-boot

提问于
浏览
7

我正在使用Spring boot和Liquibase . 使用此网址作为指南

http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

pom.xml 中,存在以下条目,以便spring boot知道liquibase .

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

并将changelog文件放在资源文件夹中 . db.changelog-master.xml

现在Spring引导首先在类路径中找到db.changelog-master.yaml并抛出这样的异常 .

找不到changelog位置:类路径资源[db / changelog / db.changelog-master.yaml

为了解决问题,我在我的课程中添加了如下所示的bean,并尝试设置changeLog proprty .

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class SampleDataJpaApplication {


@Autowired
LiquibaseProperties properties;
@Autowired
private DataSource dataSource;

@Bean
public SpringLiquibase liquibase() {
    SpringLiquibase liquibase = new SpringLiquibase();
    properties.setChangeLog("classpath:/db/changelog/db.changelog-master.xml");
    liquibase.setChangeLog(this.properties.getChangeLog());
    liquibase.setContexts(this.properties.getContexts());
    liquibase.setDataSource(this.dataSource);
    liquibase.setDefaultSchema(this.properties.getDefaultSchema());
    liquibase.setDropFirst(this.properties.isDropFirst());
    liquibase.setShouldRun(this.properties.isEnabled());
    return liquibase;
}

public static void main(String[] args) throws Exception {
    Logger logger = LoggerFactory.getLogger("SampleDataJpaApplication");
    SpringApplication springApplication = new SpringApplication();
    springApplication.run(SampleDataJpaApplication.class, args);
}

}

但它没有消息 .

org.springframework.beans.factory.BeanCreationException:创建名为'sampleDataJpaApplication'的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties sample.data.jpa.SampleDataJpaApplication.properties;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者 . 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}引起:org.springframework.beans.factory.BeanCreationException:无法自动装配字段:org.springframework.boot.autoconfigure.liquibase . LiquibaseProperties sample.data.jpa.SampleDataJpaApplication.properties;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖项找到类型[org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties]的限定bean:期望至少有一个bean可以作为此依赖项的autowire候选者 . 依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}

请在这里提供输入,为什么我得到此异常或是否有任何其他可用的方法来覆盖相同的类,以便我可以更改liquibase属性的changeLog属性 .

1 回答

  • 10

    我'm not entirely sure what the exact runtime path to your change log is, but why don'你只是使用 application.properties 中的"liquibase.*"属性?你应该可以省略 Liquibase @Bean 并让Boot为你做 .

    如果您希望添加自己的 Liquibase @Bean ,请接受提示并确保定义 LiquibaseProperties bean(例如,通过声明 @EnableConfigurationProperties(LiquibaseProperties.class) ) .

相关问题