Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yml config files "Inheritance" with Spring boot

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?

like image 874
tomer.z Avatar asked Jan 18 '18 21:01

tomer.z


People also ask

Can we use application properties and application Yml together in same Spring Boot project?

Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration.

What are in the YML files of Spring Boot?

YAML is a data serialization language that is often used for writing configuration files. So YAML configuration file in Spring Boot provides a very convenient syntax for storing logging configurations in a hierarchical format. The application.

Does Spring Boot support using both properties and YAML files at the same time?

As well as Java properties files, we can also use YAML-based configuration files in our Spring Boot application. YAML is a convenient format for specifying hierarchical configuration data.


1 Answers

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.

like image 125
Szymon Stepniak Avatar answered Sep 22 '22 01:09

Szymon Stepniak