spring 个 .yaml 文件。如何设置配置属性?

spring profiles in .yaml. How do you set up configuration properties?

我是 spring 新手,也是 .yaml 新手,我开始用谷歌搜索答案(其中很多已经过时或令人困惑)。

目前我有一个 application.yaml 看起来像这样

spring:
  profiles.active: TEST
---
spring:
  profiles: DEV
logging:
  level:
    org.springframework.web: INFO
    se.some.url: DEBUG
api:
  url:
     one: test.url
    two : test.url
certification:
  policies:
      one : 0.0.0.0.0
      two : 0.0.30.0

---
spring:
  profiles: TEST
logging:
  level:
    org.springframework.web: INFO
    se.some.url: DEBUG
api:
  url:
     one: test.url
    two : test.url
certification:
  policies:
      one : 0.0.0.0.0
      two : 0.0.30.0

我需要能够在我的代码中使用 certification.policies 和 api.url 的值,并确保根据配置文件加载的所有内容都处于活动状态。

我知道需要创建配置 class。

但是应该使用什么注解呢? 如何设置配置文件? 我如何获得价值?

感谢您的帮助!

你应该阅读 this documentation about externalized configuration

使用 @ConfigurationProperties("some-property") 告诉 Spring 使用 .yml 文件中配置的值初始化字段。

可以在启动 jar 时指定活动配置文件。你可以例如通过命令行指定活动配置文件:--spring.profiles.active=dev,hsqldbSee the documentation for more information.