java - 在 application.properties 和 application.yaml 中合并相同的属性
问题描述
我有两个属性文件:application.properties:
spring.datasources[0].url = jdbc:postgresql://localhost:5432/earch
spring.datasources[0].username = postgres
spring.datasources[0].password =
spring.dataSources[0].driver-class-name=org.postgresql.Driver
spring.dataSources[0].liquibase.enabled=false
spring.dataSources[1].tenantId=db4
spring.dataSources[1].url=jdbc:mysql://localhost:3306/test_liquibase?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.dataSources[1].username=root
spring.dataSources[1].password=
spring.dataSources[1].driver-class-name=com.mysql.cj.jdbc.Driver
spring.dataSources[1].spring.jpa.hibernate.connection.charset=utf8
spring.dataSources[1].spring.jpa.hibernate.connection.useUnicode=true
spring.dataSources[1].spring.jpa.hibernate.ddl-auto=validate
spring.dataSources[1].spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.dataSources[1].liquibase.enabled=true
spring.dataSources[1].liquibase.change-log=classpath:db/master/changelog/db.changelog-master.yaml
和 application.yaml:
spring:
dataSources:
- tenantId: db5
url: jdbc:postgresql://localhost:5432/db4
username: postgres
password:
driver-class-name: org.postgresql.Driver
liquibase:
enabled: true
change-log: classpath:db/yaml-change/changelog/db.changelog-master.yaml
和配置文件:
@Bean(name = "dataSources")
@Primary
public Map<Object, Object> getDataSources(DataSourceProperties dataSourceProperties) {
return dataSourceProperties.getDataSources().stream().map(dataSourceProperty -> {
DataSource dataSource = DataSourceBuilder.create()
.url(dataSourceProperty.getUrl())
.username(dataSourceProperty.getUsername())
.password(dataSourceProperty.getPassword())
.driverClassName(dataSourceProperty.getDriverClassName())
.build();
return new TenantIdDataSource(dataSourceProperty.getTenantId(), dataSource);
}).collect(Collectors.toMap(TenantIdDataSource::getTenantId, TenantIdDataSource::getDataSource));
}
现在只解析 yaml 属性。如果删除 yaml 属性,则从 application.properties 解析属性
是否可以将此属性从两个文件合并到 dataSourceProperties?
解决方案
创建相同的 DataSourceProperties 类,例如 DSProps:
@Data
@Component
@ConfigurationProperties(prefix = "spring1")
@PropertySource({"application.yml"})
public class DSProps {
private List<DataSourceProperty> dataSources = new LinkedList<>();
}
@Data
@Component
@ConfigurationProperties(prefix = "spring")
@PropertySource({"application.properties"})
public class DataSourceProperties {
private List<DataSourceProperty> dataSources = new LinkedList<>();
}
并在 getDataSources 方法中添加一行:
dataSourceProperties.getDataSources().addAll(dsProps.getDataSources());
当然在 application.yaml 前缀必须不同
推荐阅读
- sql - 特别是在每个日期的 10 天内重叠的日期
- ruby-on-rails - 如何使用 ActiveStorage 映射已上传的 s3 图像
- sql - 支持大数的自然排序
- excel - 在 VBA 中执行用户窗体控件时对象不支持此属性错误
- python - 比较三个函数并绘制特定区域
- javascript - 如何使用 ssh2-promise 制作交互式 shell
- python - 使用公共类复合体跟踪 e^(ix)[exp(ix)]
- ruby-on-rails - Rails 如何与带有时间戳的日期时间进行比较
- python-3.x - 子集一个不包含 Python 中其他列表中的字符串的列表
- webstorm - 通过 */css*/`.styles{}` 为 JS 中的内联 css 注入 WebStorm 语言