Sub topic 13
Importing extra configuration with spring.config.import
This chapter assumes you know how application.yaml is loaded, how property precedence works, and how profile-specific files are chosen. Here you will add more configuration sources to an application without changing the main file.
What the property does
spring.config.import tells Boot to load additional configuration after the file that declares it. The extra source can be a file, a classpath resource, a directory, or a mounted secrets tree. You can use it to keep machine-specific values out of version control or to pull in a shared file.
spring:
config:
import: "optional:file:./local-overrides.yaml"Boot inserts the imported content directly below the document that names it. Because later sources win, imported values override the values in the importing document. A developer can put app.mail.port: 2525 in an uncommitted local-overrides.yaml and it replaces the committed default.
Location prefixes
| Prefix or form | Meaning |
|---|---|
| `file:` | A path on the file system |
| `classpath:` | A resource packaged in the jar |
| `optional:` | Continue starting if the location is missing |
| `configtree:` | A directory in which each file name is a key and its content is the value |
A location that ends in / is treated as a directory. You can list several locations as a YAML list or as a comma-separated string. Boot loads them in order, so a later entry overrides an earlier one.
Optional versus required
Without optional:, a missing location stops the application at startup with a ConfigDataLocationNotFoundException. That is the right behavior when the file is essential. For a developer's local override file, add optional: so the application still starts on machines that do not have it.
Mounted secrets with a config tree
Container platforms such as Kubernetes often mount secrets as files, one file per secret. If /etc/secrets/ contains a file named app.mail.password, import the directory as a config tree:
spring:
config:
import: "optional:configtree:/etc/secrets/"Boot reads the file content as the value of the property named by the file. The secret never appears in your repository or in the jar.
Common errors
- Missing
optional:. The application fails on any machine that lacks the file. - Expecting the main file to win. The imported file overrides the importing one. If a value seems ignored, check what was imported after it.
- Relative paths. Locations are resolved from the working directory for
file:and from the importing file's location for relative paths. Run the jar from a different directory andfile:./local-overrides.yamlpoints elsewhere. - Placeholders in the location. Placeholders such as
${HOME}are allowed in the import location, but they resolve only from sources already loaded.
Exercise
Your application.yaml sets app.mail.port to 1025 and imports "optional:file:./local-overrides.yaml", which sets app.mail.port to 2525. The override file does not exist on the build server. What port is used there, and what happens if you remove the optional: prefix?
Show answer
On the build server the file is skipped, so the port is 1025. Without optional:, the application fails at startup because the required location cannot be found. On a machine where the file exists, the port is 2525, because imported values override the importing file.