Spring-boot with Liquibase Overloading Property

Refresh

November 2018

Views

15k time

7

I am using Spring boot and Liquibase. Using this url as guidelines

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

In pom.xml, the below entry is present so that spring boot knows about liquibase.

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

and put the changelog file in resources folder. db.changelog-master.xml

Now Spring boot first tring to find db.changelog-master.yaml in classpath and throwing the exception like this.

Cannot find changelog location: class path resource [db/changelog/db.changelog-master.yaml

To Fix the Issue, I have added the bean like below in my class and tried to set 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);
}

}

but it is failing with the message.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sampleDataJpaApplication': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties sample.data.jpa.SampleDataJpaApplication.properties; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties sample.data.jpa.SampleDataJpaApplication.properties; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Please provide the inputs here, why i am getting this exception or Is there any any other available way to override the same class so that i can change the changeLog property of liquibase properties.

1 answers

10

Я не совсем уверен , что точный путь выполнения на ваш журнал изменений, но почему бы вам не просто использовать «LiquiBase. *» Свойства в application.properties? Вы должны быть в состоянии оставить вне Liquibase @Beanи пусть загрузки сделать это для вас.

Если вы хотите добавить вас есть Liquibase @Beanто намек и убедитесь , что вы определяете LiquibasePropertiesкомпонент, а (например , путем декларирования @EnableConfigurationProperties(LiquibaseProperties.class)).