我可以将 liquibase.properties 与 Spring 配置一起使用吗?

问题描述 投票:0回答:1

我尝试了很长时间来找出一种在 Spring Boot 的

preserveSchemaCase
中设置
application.yaml
的方法,直到有人指出只有部分设置子集可以通过 Spring 获得!

因此,我尝试将一些配置移至

liquibase.properties
,但它没有执行任何操作。一些搜索让我相信使用 Spring Boot 时实际上并没有获取 liquibase 配置文件。是这样吗?这是否意味着设置 Spring 未公开的这些 props 只能通过代码来实现?正如System.setProperty("liquibase.preserveSchemaCase", "true")
    

spring-boot liquibase
1个回答
0
投票

Spring Boot的Liquibase bean配置:

https://github.com/spring-projects/spring-boot/blob/3d5cdb7715397db2b4b3260544624d0f919315b4/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration .java#L98

Spring Boot 支持的 Liquibase 属性(以

spring.liquibase

为前缀):

https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseProperties .java
如果您想设置 Spring 未提供的其他属性,您可以选择手动创建 Liquibase bean:创建一个 Configuration 类并通过

liquibase.properties

注释将

@PropertySource
链接到此类,然后声明一个 Liquibase这个班的豆子。
@Configuration
@PropertySource("classpath:liquibase.properties")
public class LiquibaseConfig {

    @Value("${preserveSchemaCase}")
    private boolean preserveSchemaCase;

    @Bean
    public SpringLiquibase liquibase() {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setChangeLog("classpath:db/changelog/db.changelog-master.xml");
        liquibase.setDataSource(dataSource()); // Set your data source here

        // ... set other properties 
        
        liquibase.setChangeLogParameters(Collections.singletonMap("preserveSchemaCase", preserveSchemaCase));

        return liquibase;
    }
}

对于 Spring 提供的 Liquibase 属性,您可以将它们全部移至 
liquibase.properties

中,也可以通过向此 LiquibaseConfig 配置类提供环境 bean 来从

application.yml
中获取它们。无论哪种方式,您都需要为每个属性手动调用 liquibase bean 上适当的 setter。
    

© www.soinside.com 2019 - 2024. All rights reserved.