Yml config files “Inheritance” with Spring boot

為{幸葍}努か 提交于 2019-12-09 09:25:30

问题


I couldn't find a straight answer online.

Do Spring Boot's yml files "inherit" from each other? I mean if I have: application.yml which has

server:
  port: 80
  host: foo

and application-profile1.yml which has only

server:
  port: 90

So if I start my Spring Boot with profile1 as active profile, will I also have server.host property set to foo?


回答1:


Yes, application.yml file has higher precedence over any application-{profile}.yml file. Properties from profile specific yml file will override values from the default application.yml file and properties that do not exist in profile specific yml file will be loaded from the default one. It applies to .properties files as well as to bootstrap.yml or bootstrap.properties.

Spring Boot documentation mentions it in 72.7 Change configuration depending on the environment paragraph:

In this example the default port is 9000, but if the Spring profile ‘development’ is active then the port is 9001, and if ‘production’ is active then it is 0.

The YAML documents are merged in the order they are encountered (so later values override earlier ones).

To do the same thing with properties files you can use application-${profile}.properties to specify profile-specific values.



来源:https://stackoverflow.com/questions/48330310/yml-config-files-inheritance-with-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!