The 30 second answer
Spring Boot loads application configuration while preparing the Environment, before creating the application context. ConfigDataEnvironment coordinates the search, using locations and names resolved by StandardConfigDataLocationResolver. It checks the default classpath and external locations, unless I customize them, and PropertySourceLoader implementations read properties or YAML into property sources. Those sources become part of the Environment, with configuration precedence deciding which value wins when the same setting appears more than once.
1. When. SpringApplication.run calls prepareEnvironment before any bean exists. A listener there runs the config-data processing, so all properties are ready before the context is created.
2. Where it searches. The default locations are hard-coded:
static {
List<ConfigDataLocation> locations = new ArrayList<>();
locations.add(ConfigDataLocation.of("optional:classpath:/;optional:classpath:/config/"));
locations.add(ConfigDataLocation.of("optional:file:./;optional:file:./config/;optional:file:./config/*/"));
DEFAULT_SEARCH_LOCATIONS = locations.toArray(new ConfigDataLocation[0]);
}ConfigDataEnvironment.java L79-L85
optional: means a missing location is not an error. You can replace the list with spring.config.location, add to it with spring.config.additional-location, or import more with spring.config.import (L57-L67).
3. Which file names and formats. The resolver uses the name application unless spring.config.name is set, and it gets the file formats from PropertySourceLoaders loaded with SpringFactoriesLoader:
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class,
resourceLoader.getClassLoader());StandardConfigDataLocationResolver.java L53-L55 and L78-L79. The registered loaders are PropertiesPropertySourceLoader and YamlPropertySourceLoader (spring.factories L8-L10).
4. Priority. Later locations override earlier ones. So ./config/application.properties next to the jar beats application.properties inside the jar. That lets operators change settings without rebuilding. Profile-specific files (application-prod.yml) always override the non-profile files.
5. The result. Each file becomes an OriginTrackedMapPropertySource in the Environment. "Origin tracked" means Spring Boot remembers the file and line of each value, which is why binding errors can show exactly where a bad value came from.
6. Imports and cloud sources. spring.config.import=optional:configtree:/etc/config/ or vault://... use the same pipeline with other resolvers and loaders.
Interview distinction: these files are not the top priority. Command-line arguments, system properties and environment variables override them.
Sources (2)
- Spring Boot reference: External application propertiesdocs.spring.io
- Spring Boot reference: Importing additional datadocs.spring.io