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

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

Merge same properties in application.properties and application.yaml

问题

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

spring.dataSources[0].tenantId=db1
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 中?

英文:

I have two properties files: 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&amp;allowPublicKeyRetrieval=true&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;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

and 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

and config file:

@Bean(name = &quot;dataSources&quot;)
    @Primary
    public Map&lt;Object, Object&gt; getDataSources(DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.getDataSources().stream().map(dataSourceProperty -&gt; {
            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));
    }

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类:

@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中,前缀必须不同。

英文:

create the same DataSourceProperties class for example, DSProps:

@Data
@Component
@ConfigurationProperties(prefix = &quot;spring1&quot;)
@PropertySource({&quot;application.yml&quot;})
public class DSProps {

    private List&lt;DataSourceProperty&gt; dataSources = new LinkedList&lt;&gt;();
}
@Data
@Component
@ConfigurationProperties(prefix = &quot;spring&quot;)
@PropertySource({&quot;application.properties&quot;})
public class DataSourceProperties {

    private List&lt;DataSourceProperty&gt; dataSources = new LinkedList&lt;&gt;();
}

and in the method getDataSources add one line:

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:

确定