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 profiles select files. It builds on the multi-document YAML and MailProperties examples from earlier chapters.
Why import configuration
Most settings belong in the application.yaml packaged in your jar. Some do not. A developer's local SMTP port, a shared team file, or a password mounted by an orchestrator should live outside the jar and outside version control.
The spring.config.import property lets one configuration document name other locations to load. Spring Boot loads each location as an extra document and merges it into the environment. This is also the standard hook that Spring Cloud integrations such as Config Server and Vault use.
A first import
The application.yaml below imports a local file. If the file is missing, startup continues.
spring:
config:
import: "optional:file:./local-overrides.yaml"A developer can then create local-overrides.yaml next to the jar and add it to .gitignore:
app:
mail:
port: 2525
sender: [email protected]MailProperties binds these values as if they had been written in application.yaml. No Java changes are needed.
Location prefixes
Each import value is a location string. The prefix selects the loader.
| Prefix | Meaning | Typical use |
|---|---|---|
| `file:` | A path on the file system, relative to the working directory or absolute | Local overrides, files in `/etc/myapp/` |
| `classpath:` | A resource on the classpath | Shared defaults packaged in a library |
| `configtree:` | A directory where each file is a property | Kubernetes or Docker secrets |
| `optional:` | Modifier placed before another prefix. A missing location is ignored | Files that exist only on some machines |
Without optional:, a missing location stops startup with a ConfigDataLocationNotFoundException. That is the right default for a file the application cannot run without. Use optional: only for files that really may be absent.
Where imported values rank
An imported document is inserted directly above the document that declared it. Its values override the importing document.
# application.yaml
app:
mail:
port: 1025
spring:
config:
import: "optional:file:./local-overrides.yaml"If local-overrides.yaml sets app.mail.port: 2525, the effective port is 2525.
The import does not rise above the stronger sources from the precedence chapter. Command-line arguments and environment variables still beat imported files. A --app.mail.port=9999 argument wins over both files.
Importing several locations
Give a list to import several locations. Locations are processed in order, and later entries override earlier ones.
spring:
config:
import:
- optional:file:./team-defaults.yaml
- optional:file:./local-overrides.yamlHere local-overrides.yaml wins over team-defaults.yaml for any key that appears in both. In a .properties file, use a comma-separated string.
A location imported more than once is processed only once. You cannot re-import a file later in the list to raise its priority.
Imports in profile documents
An import is a normal property, so it can sit inside a document guarded by spring.config.activate.on-profile. The import happens only when that profile is active.
app:
mail:
host: localhost
port: 1025
---
spring:
config:
activate:
on-profile: prod
import: "configtree:/run/secrets/"In prod the secrets directory is mandatory. In other profiles it is never read. Because the prod import has no optional: prefix, a missing directory fails the prod startup immediately. That is usually what you want for credentials.
Config trees for secrets
Container platforms often mount secrets as a directory of small files. A config tree turns each file into one property. The file path below the root, with / replaced by ., becomes the key. The file content becomes the value.
Suppose the mounted directory looks like this:
/tmp/demo-secrets/
app/
mail/
password (contains: s3cret)With the import in place, Spring Boot defines a property named app.mail.password with the value s3cret. The following commands create the tree and start the application with the import set through an environment variable:
mkdir -p /tmp/demo-secrets/app/mail
printf 's3cret' > /tmp/demo-secrets/app/mail/password
SPRING_CONFIG_IMPORT=optional:configtree:/tmp/demo-secrets/ java -jar app.jarSPRING_CONFIG_IMPORT works through relaxed binding, so you can add the import at deploy time without editing any packaged file. printf writes the value without a trailing newline, which keeps the password free of an extra character. A directory location must end with /.
You can then read the value like any other property, for example with @Value("${app.mail.password}").
Import versus other location properties
Two other properties also control where files come from. They are easy to confuse with spring.config.import.
| Property | Effect | Where it is set |
|---|---|---|
| `spring.config.import` | Adds extra documents. The standard locations still load | Inside a config file, or any property source |
| `spring.config.additional-location` | Adds search locations that are checked after the defaults, so they override them | Command line or environment |
| `spring.config.location` | Replaces the default search locations | Command line or environment |
Setting spring.config.location without including the default locations means classpath:/application.yaml is no longer read. Teams often hit this when they set it to a single directory and wonder why packaged defaults vanished. Use spring.config.import or additional-location when you want to add files.
Common errors
- Startup fails with
ConfigDataLocationNotFoundException. The location does not exist and has nooptional:prefix. Check the path, and check that a directory location ends with/. - The imported value is ignored. A stronger source, such as an environment variable or command-line argument, defines the same key. Inspect the winning source with the techniques from the chapter on inspecting configuration.
- A relative
file:path resolves to the wrong place. It is relative to the working directory of the process, not to the jar. An IDE, a service manager and a container may each start the process in a different directory. Use absolute paths in deployed environments. - An import inside a profile document never runs. The profile is not active. Remember that the document's
on-profilecondition applies to everything in it, includingspring.config.import. - A file without an extension is not parsed. Boot picks a parser from the extension. Add an extension hint in brackets, for example
file:/etc/myapp/settings[.yaml].
Exercise
Your application.yaml sets app.mail.port: 1025 and imports optional:file:./local-overrides.yaml, which sets app.mail.port: 2525. You start the application with --app.mail.port=9999 and the import file exists. What port does MailProperties see, and what happens to startup if you delete local-overrides.yaml?
Show answer
It sees 9999, because command-line arguments rank above both files. If you delete the file, startup still succeeds because of optional:. The port stays 9999 here because of the argument. Without the argument it would fall back to 1025.
Exercise
You import configtree:/run/secrets/ without optional: in the prod document, and the directory is missing on a staging server running prod. What happens, and how would you change the behavior if staging should start anyway?
Show answer
Startup fails with ConfigDataLocationNotFoundException, because the location is mandatory. To allow staging to start, change the import to optional:configtree:/run/secrets/. A better option is to give staging its own profile so that only real production requires the directory.