合并application.properties和application.yaml中相同的属性。

huangapple go评论106阅读模式
英文:

Merge same properties in application.properties and application.yaml

问题

我有两个属性文件:application.properties:

  1. spring.dataSources[0].tenantId=db1
  2. spring.datasources[0].url=jdbc:postgresql://localhost:5432/earch
  3. spring.datasources[0].username=postgres
  4. spring.datasources[0].password=
  5. spring.dataSources[0].driver-class-name=org.postgresql.Driver
  6. spring.dataSources[0].liquibase.enabled=false
  7. spring.dataSources[1].tenantId=db4
  8. spring.dataSources[1].url=jdbc:mysql://localhost:3306/test_liquibase?createDatabaseIfNotExist=true&allowPublicKeyRetrieval=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  9. spring.dataSources[1].username=root
  10. spring.dataSources[1].password=
  11. spring.dataSources[1].driver-class-name=com.mysql.cj.jdbc.Driver
  12. spring.dataSources[1].spring.jpa.hibernate.connection.charset=utf8
  13. spring.dataSources[1].spring.jpa.hibernate.connection.useUnicode=true
  14. spring.dataSources[1].spring.jpa.hibernate.ddl-auto=validate
  15. spring.dataSources[1].spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  16. spring.dataSources[1].liquibase.enabled=true
  17. spring.dataSources[1].liquibase.change-log=classpath:db/master/changelog/db.changelog-master.yaml

和 application.yaml:

  1. spring:
  2. dataSources:
  3. - tenantId: db5
  4. url: jdbc:postgresql://localhost:5432/db4
  5. username: postgres
  6. password:
  7. driver-class-name: org.postgresql.Driver
  8. liquibase:
  9. enabled: true
  10. change-log: classpath:db/yaml-change/changelog/db.changelog-master.yaml

还有配置文件:

  1. @Bean(name = "dataSources")
  2. @Primary
  3. public Map<Object, Object> getDataSources(DataSourceProperties dataSourceProperties) {
  4. return dataSourceProperties.getDataSources().stream().map(dataSourceProperty -> {
  5. DataSource dataSource = DataSourceBuilder.create()
  6. .url(dataSourceProperty.getUrl())
  7. .username(dataSourceProperty.getUsername())
  8. .password(dataSourceProperty.getPassword())
  9. .driverClassName(dataSourceProperty.getDriverClassName())
  10. .build();
  11. return new TenantIdDataSource(dataSourceProperty.getTenantId(), dataSource);
  12. }).collect(Collectors.toMap(TenantIdDataSource::getTenantId, TenantIdDataSource::getDataSource));
  13. }

现在只解析了 YAML 属性。如果删除 YAML 属性,则从 application.properties 中解析属性。是否可能将这两个文件中的属性合并到 dataSourceProperties 中?

英文:

I have two properties files: application.properties:

  1. spring.datasources[0].url = jdbc:postgresql://localhost:5432/earch
  2. spring.datasources[0].username = postgres
  3. spring.datasources[0].password =
  4. spring.dataSources[0].driver-class-name=org.postgresql.Driver
  5. spring.dataSources[0].liquibase.enabled=false
  6. spring.dataSources[1].tenantId=db4
  7. spring.dataSources[1].url=jdbc:mysql://localhost:3306/test_liquibase?createDatabaseIfNotExist=true&amp;allowPublicKeyRetrieval=true&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC
  8. spring.dataSources[1].username=root
  9. spring.dataSources[1].password=
  10. spring.dataSources[1].driver-class-name=com.mysql.cj.jdbc.Driver
  11. spring.dataSources[1].spring.jpa.hibernate.connection.charset=utf8
  12. spring.dataSources[1].spring.jpa.hibernate.connection.useUnicode=true
  13. spring.dataSources[1].spring.jpa.hibernate.ddl-auto=validate
  14. spring.dataSources[1].spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
  15. spring.dataSources[1].liquibase.enabled=true
  16. spring.dataSources[1].liquibase.change-log=classpath:db/master/changelog/db.changelog-master.yaml

and application.yaml:

  1. spring:
  2. dataSources:
  3. - tenantId: db5
  4. url: jdbc:postgresql://localhost:5432/db4
  5. username: postgres
  6. password:
  7. driver-class-name: org.postgresql.Driver
  8. liquibase:
  9. enabled: true
  10. change-log: classpath:db/yaml-change/changelog/db.changelog-master.yaml

and config file:

  1. @Bean(name = &quot;dataSources&quot;)
  2. @Primary
  3. public Map&lt;Object, Object&gt; getDataSources(DataSourceProperties dataSourceProperties) {
  4. return dataSourceProperties.getDataSources().stream().map(dataSourceProperty -&gt; {
  5. DataSource dataSource = DataSourceBuilder.create()
  6. .url(dataSourceProperty.getUrl())
  7. .username(dataSourceProperty.getUsername())
  8. .password(dataSourceProperty.getPassword())
  9. .driverClassName(dataSourceProperty.getDriverClassName())
  10. .build();
  11. return new TenantIdDataSource(dataSourceProperty.getTenantId(), dataSource);
  12. }).collect(Collectors.toMap(TenantIdDataSource::getTenantId, TenantIdDataSource::getDataSource));
  13. }

now parsed only yaml properties. If drop yaml prop, parsed properties from application.properties

is it possible to merge this properties from two files to dataSourceProperties?

答案1

得分: 1

为例,创建与DataSourceProperties类相同的DSProps类:

  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = "spring1")
  4. @PropertySource({"application.yml"})
  5. public class DSProps {
  6. private List<DataSourceProperty> dataSources = new LinkedList<>();
  7. }
  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = "spring")
  4. @PropertySource({"application.properties"})
  5. public class DataSourceProperties {
  6. private List<DataSourceProperty> dataSources = new LinkedList<>();
  7. }

并且在方法getDataSources中添加一行:

  1. dataSourceProperties.getDataSources().addAll(dsProps.getDataSources());

当然,在application.yaml中,前缀必须不同。

英文:

create the same DataSourceProperties class for example, DSProps:

  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = &quot;spring1&quot;)
  4. @PropertySource({&quot;application.yml&quot;})
  5. public class DSProps {
  6. private List&lt;DataSourceProperty&gt; dataSources = new LinkedList&lt;&gt;();
  7. }
  1. @Data
  2. @Component
  3. @ConfigurationProperties(prefix = &quot;spring&quot;)
  4. @PropertySource({&quot;application.properties&quot;})
  5. public class DataSourceProperties {
  6. private List&lt;DataSourceProperty&gt; dataSources = new LinkedList&lt;&gt;();
  7. }

and in the method getDataSources add one line:

  1. dataSourceProperties.getDataSources().addAll(dsProps.getDataSources());

of course in the application.yaml prefix must be different

huangapple
  • 本文由 发表于 2020年9月16日 20:33:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63920200.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定