春季根据配置文件组合选择属性来源

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

Spring choose property source by a combination of profiles

问题

Sure, here's the translated content:

  1. 我有多个由Spring配置文件驱动的环境例如 `application-int.yml``application-dev.yml` 内容类似
  2. - `application-int.yml`
  3. ```yaml
  4. ws:
  5. endpoint: http://my-soap-int-endpoint.com/
  6. mock: http://my-soap-int-mock-endpoint.com/
  • application-dev.yml
    1. ws:
    2. endpoint: http://my-soap-dev-endpoint.com/
    3. mock: http://my-soap-dev-mock-endpoint.com/

我的目标是基于环境名称和是否包含 mock 配置,使用以下属性之一:

  • 对于 dev 环境,使用 application-dev.yml 中的 ws.endpoint
  • 对于 int 环境,使用 application-int.yml 中的 ws.endpoint
  • 对于 dev mock 环境,使用 application-dev.yml 中的 ws.mock
  • 对于 int mock 环境,使用 application-int.yml 中的 ws.mock

我需要将该值解析到单个变量 url 中:

  1. @Configuration
  2. public class SoapConfiguration {
  3. @Value("${wsUrl}") // 基于两个属性
  4. private String wsUrl;
  5. }

我希望避免仅基于 @Profile 创建复杂的抽象配置类层次结构。此外,我需要将模拟和非模拟变量都放在一个通用的属性文件中。有没有既可读又易于维护的好方法呢?

  1. <details>
  2. <summary>英文:</summary>
  3. I have multiple environments driven by Spring profiles, such as `application-int.yml`, `application-dev.yml` etc with similar content:
  4. - `application-int.yml`

ws:
endpoint: http://my-soap-int-endpoint.com/
mock: http://my-soap-int-mock-endpoint.com/

  1. - `application-dev.yml`

ws:
endpoint: http://my-soap-dev-endpoint.com/
mock: http://my-soap-dev-mock-endpoint.com/

  1. My goal is to use the following property based on **both** the environment name **and** whether the `mock` profile is included:
  2. - `ws.endpoint` from `application-dev.yml` for `dev` profiles
  3. - `ws.endpoint` from `application-int.yml` for `int` profiles
  4. - `ws.mock` from `application-dev.yml` for `dev mock` profiles
  5. - `ws.mock` from `application-int.yml` for `int mock` profiles
  6. I need to parse that value into a single variable `url`:

@Configuration
public class SoapConfiguration {

  1. @Value(&quot;???&quot;) // based on 2 properties
  2. private String wsUrl;

}

  1. I would like to avoid complicated hierarchy of abstract configuration classes *only* based on `@Profile`. Moreover, I need to keep to keep *both* variables (mock and non-mock) in a common propery file.
  2. Is there a nice way that is both readable and maintainable?
  3. </details>
  4. # 答案1
  5. **得分**: 1
  6. 你可以在构造函数中设置`wsUrl`。这不是非常优雅的解决方案,但它可以工作。
  7. `Environment` bean注入到你的`SoapConfiguration`中,然后检查是否激活了`mock`配置文件。
  8. 代码示例:
  9. ```java
  10. @Configuration
  11. public class SoapConfiguration {
  12. private final String wsUrl;
  13. public SoapConfiguration(Environment environment) {
  14. if (Arrays.asList(environment.getActiveProfiles()).contains("mock")) {
  15. this.wsUrl = environment.getProperty("ws.mock");
  16. } else {
  17. this.wsUrl = environment.getProperty("ws.endpoint");
  18. }
  19. }
  20. }
英文:

You can set wsUrl in constructor. It's not so elegant solution but it works.
Inject Environment bean to your SoapConfiguration and check is mock profile active.<br>
Code example:

  1. @Configuration
  2. public class SoapConfiguration {
  3. private final String wsUrl;
  4. public SoapConfiguration(Environment environment) {
  5. if (Arrays.asList(environment.getActiveProfiles()).contains(&quot;mock&quot;)) {
  6. this.wsUrl = environment.getProperty(&quot;ws.mock&quot;);
  7. } else {
  8. this.wsUrl = environment.getProperty(&quot;ws.endpoint&quot;);
  9. }
  10. }
  11. }

huangapple
  • 本文由 发表于 2020年9月7日 18:46:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63776052.html
匿名

发表评论

匿名网友

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

确定