【发布时间】:2021-01-03 07:03:48
【问题描述】:
我有两个属性文件: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 属性。如果 drop yaml prop,则从 application.properties 解析属性
是否可以将此属性从两个文件合并到 dataSourceProperties?
【问题讨论】:
-
您可以根据需要配置数据源。在您的情况下,在 yaml 文件中配置数据源就足够了。在不同的地方定义相同的设置看起来有点奇怪和多余。我的建议是 - 尝试将您的设置与您的代码分开,在 application.yaml 或 application.properties 等中。
-
你可以尝试使用这个解决方案:stackoverflow.com/questions/5598314/…
-
您也可以尝试重命名一个文件(例如
application1.properties)。原因:stackoverflow.com/a/25862357/1943863 -
我不能重命名任何文件,除了这个应用程序文件包含许多其他键。这个项目是遗留的
标签: java spring-boot