Sub topic 10
Profile-specific files and multi-document YAML
This chapter assumes you can read YAML and .properties syntax, and that you know what a Spring profile is and how to activate one. Here you will see two ways to keep per-environment values in files.
Two ways to scope settings to a profile
Spring Boot can attach settings to a profile in two ways:
- Profile-specific files. A file named
application-{profile}.yamlorapplication-{profile}.propertiesloads only when that profile is active. - Multi-document files. One file holds several documents, and each document can be limited to a profile.
Both produce the same result. Pick one style per project so readers know where to look.
Profile-specific files
With the prod profile active, Boot loads application.yaml first and then application-prod.yaml. Values in the profile file replace matching keys from the base file. Keys that the profile file does not mention keep their base values.
# application-prod.yaml
app:
mail:
host: smtp.example.com
port: 587If application.yaml also sets app.mail.sender and app.mail.retry-attempts, those values still apply in prod.
Multi-document YAML
A line containing only --- splits a YAML file into documents. Boot reads them from top to bottom. A document that sets spring.config.activate.on-profile is used only when that profile is active.
app:
mail:
host: localhost
port: 1025
sender: [email protected]
retry-attempts: 3
---
spring:
config:
activate:
on-profile: prod
app:
mail:
host: smtp.example.com
port: 587The first document has no condition, so it always applies. The second applies only under prod and overrides host and port.
In a .properties file, a line containing only #--- plays the same role, and you put spring.config.activate.on-profile=prod in the document that follows it.
Choosing between them
| Approach | Strength | Cost |
|---|---|---|
| `application-prod.yaml` | Short files, easy to find, easy to keep out of version control | Settings for one feature are spread across files |
| Multi-document file | All environments visible in one place | The file grows long |
Common errors
- Conditions inside profile files. Do not put
spring.config.activate.on-profileinapplication-prod.yaml. Boot rejects it, because the file name already sets the profile. - Order inside one file. A later document overrides an earlier one. If the
proddocument comes first, the unconditional document after it replaces its values. - Several active profiles. With
--spring.profiles.active=prod,eu, the profile listed last wins on conflicting keys. - A misspelled profile name. Nothing fails. The values are skipped, and you get the base values.
Exercise
A file contains a base document with app.mail.port: 1025, then a document with on-profile: prod and app.mail.port: 587, then a third document with no condition and app.mail.port: 2525. Which port is used when prod is active?
Show answer
2525. Documents are applied from top to bottom, and the unconditional third document comes after the prod document, so it overrides it. Moving the prod document to the end gives 587.