简体   繁体   English

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

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

I am a spring newbie and a .yaml newbie as well and I´m starting to get cross eyed googling answers(a lot of them are very outdated or just confusing). 我既是春季新手,也是.yaml新手,并且我开始cross目结舌地搜索答案(其中很多都非常过时或令人困惑)。

Currently I have a application.yaml that looks like this 目前我有一个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

I need to be able to use the values of the certification.policies and api.url in my code and make sure everything loads depending on with profile is active. 我需要能够在我的代码中使用certificate.policies和api.url的值,并确保根据配置文件加载的所有内容均处于活动状态。

I do get that a config class needs to be created. 我确实知道需要创建一个配置类。

But what annotation should be used? 但是应该使用什么注释? How do I set the profile? 如何设置个人资料? How do I get the value? 我如何获得价值?

Grateful for any help! 感谢您的帮助!

You should read this documentation about externalized configuration . 您应该阅读有关外部化配置的文档

With @ConfigurationProperties("some-property") you tell Spring to initialize the fields with the values configured in your .yml file. 使用@ConfigurationProperties("some-property")您告诉Spring使用.yml文件中配置的值初始化字段。

The active profile can be specified when starting your jar. 启动jar时可以指定活动配置文件。 You can eg specify the active profile via command line: --spring.profiles.active=dev,hsqldb . 您可以例如通过命令行指定活动配置文件:-- --spring.profiles.active=dev,hsqldb See the documentation for more information. 请参阅文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot YAML 配置文件配置文件 - Spring Boot YAML profiles configuration file 多个 Spring 配置文件的属性解析(yaml 配置) - Property resolving for multiple Spring profiles (yaml configuration) 您如何在引导文件中正确设置不同的 Spring 配置文件(用于 Spring Boot 以针对不同的云配置服务器)? - How do you properly set different Spring profiles in bootstrap file (for Spring Boot to target different Cloud Config Servers)? 如何在 Spring Boot 应用程序的 application.properties 文件中设置 MyBatis 配置属性? - How do I set MyBatis configuration properties in an application.properties file in a Spring Boot application? Spring Boot Yaml配置:类型化属性列表 - Spring Boot Yaml configuration: list of typed properties 如何使用配置文件外部化Spring Boot中的配置? - How to externalize configuration in Spring Boot using profiles? Spring Boot:从jar包中排除配置文件的配置属性 - Spring Boot: Exclude configuration properties for profiles from jar package 如何使用 spring boot + .yaml 创建配置文件? - How can I create profiles with spring boot + .yaml? Spring配置文件配置 - Spring profiles configuration 是否可以使用具有 Spring 引导配置属性的 YAML 序列? - Is it possible to user a YAML sequence with Spring Boot configuration properties?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM