Sub topic 3
Property sources and which value wins
This chapter assumes you can read YAML and .properties files and have a running Spring Boot application. It builds on the application.yaml file from the previous chapter.
Property sources
Spring Boot does not read configuration from one place. It builds an ordered list of property sources, and each source is a named collection of key-value pairs. When the same key appears in more than one source, the source higher in the list wins. Keys that appear in only one source are simply merged in.
The order, highest priority first
| Priority | Source |
|---|---|
| 1 | `properties` on `@SpringBootTest` and `@TestPropertySource` |
| 2 | Command-line arguments such as `--app.mail.port=2525` |
| 3 | `SPRING_APPLICATION_JSON` (inline JSON in an environment variable) |
| 4 | Java system properties (`-Dapp.mail.port=2525`) |
| 5 | OS environment variables |
| 6 | Profile-specific files, such as `application-prod.yaml` |
| 7 | General files, such as `application.yaml` |
| 8 | `@PropertySource` on `@Configuration` classes |
| 9 | Defaults set with `SpringApplication.setDefaultProperties` |
This list is simplified. The full list in the Spring Boot reference has more entries, such as JNDI and servlet init parameters. Two rules cover most surprises:
- Within the file level, profile-specific files beat general files. A file outside the jar beats the same file inside the jar.
- Anything you pass at launch time beats anything packaged in the jar. That is why the same jar can run in several environments.
A worked example
Suppose application.yaml sets app.mail.port to 1025, and the prod profile overrides it to 587. You then launch the application like this:
APP_MAIL_PORT=2525 java -jar app.jar --spring.profiles.active=prod --app.mail.port=3000All four values apply to the same key. The effective port is 3000, because the command line is the highest source present. Remove --app.mail.port=3000 and it becomes 2525. Remove the environment variable too and it becomes 587. Drop the prod profile and you get 1025.
Only the conflicting key is affected. app.mail.sender and app.mail.retry-attempts still come from application.yaml because no higher source sets them.
Common errors
- A file edit has no effect. A higher source is overriding it. A stale environment variable in your shell or IDE run configuration is the usual cause.
- A value from
application-prod.yamlis ignored. Theprodprofile is not active. Profile-specific files load only when their profile is active. - You expect lists to merge. They do not. A list in a higher source replaces the whole list from a lower one.
- Properties in
@PropertySourcelose toapplication.yaml. They sit lower in the order, so do not use them for values that files should override.
Exercise
A jar contains application.yaml with server.port=8080. The shell has SERVER_PORT=9000 exported, and the application starts with -Dserver.port=7000. Which port does the application use, and why?
Show answer
7000. Java system properties (priority 4) rank above OS environment variables (priority 5), and both rank above the application.yaml packaged in the jar.