Video in progress
How is a profile-specific file like application-dev.yml loaded, and how does it merge with the main file?
application.yml is read first, then the active profile's application-{profile}.yml is laid over it: identical keys come from the profile file, the rest from the main file. With several profiles, the last one wins.
📖 Answer
# application.yml
app:
mail-enabled: true
page-size: 20
spring:
datasource:
url: jdbc:postgresql://db:5432/shop
# application-dev.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/shop_dev
app:
mail-enabled: false
With --spring.profiles.active=dev the result is: url and mail-enabled from dev, page-size: 20 from the main file. The file isn't replaced, it's merged key by key.
Where people go wrong:
spring.profiles.active=dev,local: sincelocalcomes later, it wins for keys present in both;- since Boot 2.4, setting
spring.profiles.activeinside a profile file is forbidden, and the application fails at startup; - lists aren't merged: if the profile file has an
allowed-originslist, it completely replaces the list in the main file; - environment variables and command-line arguments take priority over the files too.
You can also write everything in one file:
app.page-size: 20
---
spring.config.activate.on-profile: dev
app.page-size: 5
To find out where a value came from, /actuator/env is handy: it shows the source for each key.
Sign up to mark studied questions and save your progress.