Yml config files "Inheritance" with Spring boot
Here is my solution.
Assume application.yml
:
spring:
profiles: default-server-config
server:
port: 9801
servlet:
context-path: '/ctp'
If I want use default-server-config
profile, and use port 8080
in my application-dev.yml
application-dev.yml
:
spring:
profiles:
include:
- default-server-config
- dev-config
---
spring:
profiles: dev-config
server:
port: 8080
Then -Dspring.profiles.active=dev
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.