Sub topic 10
Profile-specific files and multi-document YAML
This chapter assumes you know the basics of application.yaml, can read YAML and .properties syntax, and know how to activate a profile with spring.profiles.active. Here you will learn the two ways Spring Boot lets you vary configuration by profile without code changes: separate files named after the profile, and several documents inside one file.
Two ways to scope configuration to a profile
A profile is a named label for an environment, such as dev or prod. Boot can attach configuration to a profile in two ways:
- Profile-specific files. A file named
application-{profile}.yamlorapplication-{profile}.propertiesis loaded only when that profile is active. - Multi-document files. One file holds several documents, and a document is applied only when a condition on the active profiles is true.
Both produce the same result. They differ in where the values live and in some rules about what each may contain.
Profile-specific files
Boot looks for application-{profile}.* in the same locations as the base file. With prod active, it loads application.yaml first and then application-prod.yaml. Values in the profile file replace values with the same key in the base file. Keys the profile file does not mention keep their base values.
# application.yaml
app:
mail:
host: localhost
port: 1025
sender: [email protected]# application-prod.yaml
app:
mail:
host: smtp.example.com
port: 587With prod active, host and port come from application-prod.yaml. sender still comes from application.yaml. Keep the base file as a complete set of working defaults and put only the differences in profile files.
Locations and ordering
Boot searches these locations. Later entries override earlier ones:
- The classpath root
- The classpath
/configpackage - The current directory
- The
config/subdirectory of the current directory - Direct child directories of
config/
Profile-specific files always override the non-specific ones. This holds whether the profile file is inside or outside the packaged jar. A config/application-prod.yaml next to the jar therefore beats everything packaged inside it.
If application.properties and application.yaml sit in the same location, the .properties file wins for any key defined in both. Pick one format per project to avoid confusing yourself.
Several active profiles
You can activate more than one profile:
java -jar app.jar --spring.profiles.active=prod,cloudBoot loads application-prod.* and then application-cloud.*. When both define a key, the last profile listed wins. Order in the list matters.
The default profile
If no profile is active, Boot activates a profile called default. Boot then loads application-default.yaml if it exists. As soon as you activate any profile, default is no longer active, and its file is skipped.
Multi-document YAML
YAML lets you put several documents in one file, separated by a line containing ---. Boot treats each document as its own property source and processes them from top to bottom. A document with spring.config.activate.on-profile is used only when that condition matches.
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 when prod is active. Because it comes later, its host and port override the first document's values.
For .properties files, use a line containing only #--- as the separator:
app.mail.host=localhost
app.mail.port=1025
#---
spring.config.activate.on-profile=prod
app.mail.host=smtp.example.com
app.mail.port=587Profile expressions
The value of on-profile can be a profile expression, not just a name:
| Expression | Matches when | Column |
|---|---|---|
| `prod` | `prod` is active | |
| `!prod` | `prod` is not active | |
| `dev \ | test` | `dev` or `test` is active |
| `prod & cloud` | both are active | |
| `(dev \ | test) & !cloud` | `dev` or `test` is active, and `cloud` is not |
Mixing & and | in one expression requires parentheses. Boot rejects a & b | c.
Order inside one file
Document order is significant, because later documents override earlier ones. Suppose a conditional document sits first and an unconditional one follows it with the same key:
spring:
config:
activate:
on-profile: prod
app:
mail:
port: 587
---
app:
mail:
port: 1025Here the unconditional second document overrides the prod value, and port is 1025 even in production. Put unconditional defaults first and conditional documents after them.
How the pieces combine
Multi-document sections and profile-specific files can coexist. Boot combines them in this order:
Profile-specific files override everything in the base file, including its matching conditional documents. The last step reflects the general precedence rules from earlier chapters: environment variables and command-line arguments beat values from files.
Restrictions to know about
Boot deliberately limits what a conditional document may contain, because the active profiles must be settled before it can decide which documents to read.
- A profile-specific file such as
application-prod.yamlcannot containspring.config.activate.on-profile. The file name already states the condition. - A document with
on-profilecannot setspring.profiles.activeorspring.profiles.include. - Profile-specific files cannot set
spring.profiles.activeeither.
Violating these rules makes the application fail at startup with an InvalidConfigDataPropertyException. Failing at startup is better than a profile silently not activating.
Choosing between the two styles
| Row | Profile-specific files | Multi-document file |
|---|---|---|
| Where values live | One file per profile | One file for everything |
| Easy to see all differences | Needs several files open | Yes, one screen |
| Can be kept outside the jar | Yes, per profile | The whole file moves together |
| Condition types | Profile name only | Full profile expressions |
| Risk | Typo in file name means silent non-loading | Wrong document order changes the result |
Separate files suit settings that ops teams edit or deploy outside the jar, because you can ship application-prod.yaml on its own. Multi-document files suit small differences you want to read next to the defaults, and any case needing an expression such as !prod.
Exercise
A project has application.yaml with app.mail.port: 1025, an application-prod.yaml with app.mail.port: 587, and an application-cloud.yaml with app.mail.port: 465. The app starts with --spring.profiles.active=cloud,prod. Which port is used, and why? What changes if you start it with --spring.profiles.active=prod,cloud?
Show answer
Port 587 is used with cloud,prod, because the last listed profile wins and application-prod.yaml is loaded after application-cloud.yaml. With prod,cloud the result is 465, because application-cloud.yaml is then loaded last.
Exercise
You want a document in application.yaml to apply whenever either dev or test is active, but not when cloud is also active. Write the on-profile value, and say where in the file the document should go relative to the unconditional defaults.
Show answer
Use spring.config.activate.on-profile: "(dev | test) & !cloud". Parentheses are required because & and | are mixed. Place the document after the unconditional defaults, because later documents override earlier ones.