Quick revision
The short answer
spring.config.import tells Spring Boot to load additional config data from the locations we list, as if it were part of the configuration. Prefixing a location with optional: means a missing location does not fail startup; file:./extra.yml imports a file; configtree:/etc/config/ imports a directory tree where each file name is a key and the file content is its value. Imported values take precedence over the document that imports them, and extensions such as Spring Cloud Config or Spring Cloud Vault add their own location prefixes.
What to remember
In this answer, we saw that spring.config.import loads additional config data from the locations we list. optional: lets a location be absent without failing startup, file: imports a single file, and configtree: imports a directory where each file name is a key and its content the value, which is how Kubernetes mounts ConfigMaps and Secrets. Imported values take precedence over the document that imports them while still ranking below environment variables and command line arguments, and libraries such as Spring Cloud Config and Vault add their own prefixes like configserver: and vault://.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
spring.config.import tells Spring Boot to load additional config data from the locations we list, as if it were part of the configuration. Prefixing a location with optional: means a missing location does not fail startup; file:./extra.yml imports a file; configtree:/etc/config/ imports a directory tree where each file name is a key and the file content is its value. Imported values take precedence over the document that imports them, and extensions such as Spring Cloud Config or Spring Cloud Vault add their own location prefixes.
Why this question is asked
Since Spring Boot 2.4, spring.config.import has been the standard way to pull configuration from places other than the default application.yml: an extra file mounted by the platform, a Kubernetes secret, a config server or a secret store. It replaced older mechanisms such as bootstrap.yml for Spring Cloud. Interviewers ask about it because almost every production deployment uses it somewhere, and the details, optional locations, precedence and config trees, decide whether a service starts and which value wins. In this answer, we will recap what config data is, look at the basic import, then optional:, file:, configtree: and the extension prefixes, and finish with how precedence works for imported values.
1A Quick Recap of Config Data
Config data is Spring Boot's name for configuration it loads from files and other locations at startup, such as application.yml in the classpath, a ./config folder or a location given with spring.config.location. These are processed before the application context is created, which is why they can affect anything, including logging and auto-configuration.
spring.config.import is a property inside config data that says: also load these locations. It can appear in application.yml, in a profile-specific document, as an environment variable SPRING_CONFIG_IMPORT or on the command line. Spring Boot processes the import, then processes any imports inside the imported files too.
2Optional Locations: optional:
By default, an import whose location does not exist stops startup with a ConfigDataLocationNotFoundException. That is the right behaviour when the file is essential. When the file only exists in some environments, such as an override mounted only in production, we mark it optional: and a missing location is simply skipped.
So optional: is a statement about the environment: we expect this location to be absent sometimes, and that is fine. Using it everywhere hides real mistakes, such as a path typed wrongly, so it should be used deliberately.
3Files and Config Trees: file: and configtree:
file: imports a single file from the file system, such as file:./extra.yml or file:/etc/app/overrides.properties. Spring Boot picks the loader from the extension; for a file without an extension, we can give a hint in brackets, such as file:/etc/app/myconfig[.yaml].
configtree: is different. It imports a directory tree in the format Kubernetes uses when it mounts ConfigMaps and Secrets as volumes: each file's name is a property key and its content is the value, and subdirectories add levels to the name. This lets a service read mounted secrets as ordinary properties without parsing files itself.
| Prefix | Imports | Example |
|---|---|---|
| (none) / classpath: | a file in the classpath | classpath:shared.yml |
| file: | a file on disk | file:./extra.yml |
| optional: | any location; missing is not an error | optional:file:./extra.yml |
| configtree: | a directory; file name = key, content = value | configtree:/etc/config/ |
| configserver: (Spring Cloud) | properties from a Config Server | configserver:http://config:8888 |
| vault:// (Spring Cloud Vault) | secrets from Vault | vault://secret/orders |
Notice that both forms of nesting work: a directory db with a file username, and a file whose name already contains a dot. A trailing slash on the location is needed for directory imports.
4Extension Prefixes From Spring Cloud and Others
The import mechanism is pluggable: a library can register a loader for its own prefix. Spring Cloud Config adds configserver:, Spring Cloud Vault adds vault://, Spring Cloud AWS adds prefixes for Secrets Manager and Parameter Store, and others exist for other platforms; check the library's documentation for the exact prefix in your version. They combine with optional: like any other location.
A list of imports is processed in order, and later imports take precedence over earlier ones, so in this example a value in local-overrides.yml beats the same key from the config server.
5How Precedence Works for Imported Values
The rule to remember is that an imported document takes precedence over the document that imports it. Spring Boot places the imported configuration just above the importing file in the list of property sources. That makes imports useful for overrides: application.yml in the jar holds defaults and imports a mounted file, and the mounted file wins.
- Command line arguments--server.port=6060
- Environment variables
SERVER_PORT=7070 - Imported config data
override.yml: 9090 - Importing config data
application.yml: 8080
4 layers, from the top down. Step through them one at a time.
Imported config data is still config data, so it ranks below environment variables, system properties and command line arguments. An imported file overrides the file that imported it, but SERVER_PORT=7070 in the environment would still win over both. The position in the importing file does not matter: an import is always processed after the document containing it, however early in the file we write it.
6Follow-up questions
What did spring.config.import replace in Spring Cloud?
The bootstrap context and bootstrap.yml, which Spring Cloud used to fetch remote configuration before the main context started. With config data imports, the remote location is just another import; the bootstrap approach still exists behind a flag in some versions, but imports are the recommended way.
Can a profile-specific document import something?
Yes. An import inside a document activated with spring.config.activate.on-profile is only processed when that profile is active, which lets prod import a mounted override while local development does not. Unlike spring.profiles.active, imports are allowed in profile-specific documents.
What happens if two imports set the same key?
The later import in the list wins, since imports are processed in order and each later one is placed above the earlier ones. To make the order obvious to readers, list the most general location first and the most specific override last.
7Conclusion
In this answer, we saw that spring.config.import loads additional config data from the locations we list. optional: lets a location be absent without failing startup, file: imports a single file, and configtree: imports a directory where each file name is a key and its content the value, which is how Kubernetes mounts ConfigMaps and Secrets. Imported values take precedence over the document that imports them while still ranking below environment variables and command line arguments, and libraries such as Spring Cloud Config and Vault add their own prefixes like configserver: and vault://.
Open with
spring.config.import tells Spring Boot to load more config data from the locations I list, and treat it as part of the configuration, before the application context is created.
Then, if they want more
It can be set in application.yml, in a profile-specific document, or as SPRING_CONFIG_IMPORT. By default a missing location fails startup, and the optional: prefix changes that, so for example optional:file:./extra.yml is skipped if the file is not there.
I use optional deliberately for things that only exist in some environments. file: imports a single file from disk, and Spring Boot picks the format from the extension. configtree: imports a directory in the layout Kubernetes uses for mounted ConfigMaps and Secrets. With configtree:/etc/config/, a file /etc/config/db/password becomes the property db.password and its content becomes the value.
For precedence, imported values win over the document that imports them, and later imports in a list win over earlier ones. So a jar's application.yml can hold defaults and import a mounted override. Imported config data still ranks below environment variables and command line arguments.
The mechanism is pluggable, so Spring Cloud Config adds configserver:, Spring Cloud Vault adds vault://, and the cloud libraries add their own prefixes. That replaced the old bootstrap.yml approach.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 2.4
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does spring.config.import do? Give examples with optional:, file: and configtree:.": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/b36e9068e7/