.yaml中的弹簧轮廓。如何设置配置属性?

时间:2019-06-17 13:12:55

标签: spring-boot configuration yaml spring-profiles

我既是春季新手,又是.yaml新手,而且我也开始cross目结舌地使用Google搜寻答案(其中很多都非常过时或令人困惑)。

目前,我有一个看起来像这样的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

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

我确实需要创建一个配置类。

但是应该使用什么注释? 如何设置个人资料? 我如何获得价值?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您应该阅读this documentation about externalized configuration

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

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