Master Spring Boot Externalized Config: Locations, Overrides & Advanced Options
This guide explains how Spring Boot 3.1.10 discovers and loads external configuration files from classpath and filesystem locations, how to customize file names and paths with spring.config.name, spring.config.location, and spring.config.additional-location, and demonstrates merging, profile-specific files, optional prefixes, wildcards, imports, and handling missing resources.
1. Externalized Configuration
Spring Boot automatically searches for application.properties and application.yml in the following locations, ordered by priority (lower items override higher ones):
Classpath root
Classpath /config directory
Current working directory
Current directory /config directory
Direct sub‑directories of /config
Example file layout:
Root application.yml :
<code>pack:
name: root name
age: 66</code>/config application.yml :
<code>pack:
name: classpath /config name</code>Injecting values:
<code>@Value("${pack.name}")
private String name;
@Value("${pack.age}")
private Integer age;</code>Result:
<code>name = classpath /config name, age = 66</code>Note: lower‑priority values (later in the list) override earlier ones, and the two files are merged rather than one replacing the other.
1.1 Change Default Configuration File Name
The default name is application . It can be changed with the spring.config.name property, e.g.:
<code>java -jar springboot-configfile-1.0.0.jar \
--spring.config.name=pack</code>1.2 Change Configuration File Location
Use spring.config.location to specify explicit locations (comma‑separated). The optional: prefix prevents startup failure if a file is missing.
<code>java -jar springboot-configfile-1.0.0.jar \
--spring.config.location=optional:classpath:/pack.yml,optional:file:f:/</code>When only a directory is given, Spring Boot appends the default file name automatically.
1.3 Additional Configuration Files
Use spring.config.additional-location to load extra files that can override values from the default locations.
<code>java -jar springboot-configfile-1.0.0.jar \
--spring.config.location=optional:classpath:/pack.yml \
--spring.config.name=pack \
--spring.config.additional-location=optional:file:f:/pack-akf.yml</code>Result shows the additional file overriding pack.name while preserving pack.age .
2. Detailed Configuration Options
2.1 Optional Locations
If a specified location does not exist, Spring Boot throws ConfigDataLocationNotFoundException . Prefixing the location with optional: makes the missing file harmless.
2.2 Wildcard Locations
Paths ending with * are treated as wildcards; all matching sub‑directories are scanned.
<code>java -jar springboot-configfile-1.0.0.jar \
--spring.config.location=optional:classpath:/pack.yml \
--spring.config.name=pack \
--spring.config.additional-location=file:f:/sb-config/*/ \
--spring.config.on-not-found=ignore</code>Example code reads values from redis and mysql configuration files loaded via the wildcard.
2.3 Profile‑Specific Files
Spring Boot also loads application-{profile}.yml files. Later profiles override earlier ones.
<code>spring:
profiles:
active:
- prod
- live</code>With pack-prod.yml and pack-live.yml , the live values replace the prod ones.
2.4 Importing Other Data
The spring.config.import property can import additional configuration files.
<code>pack:
name: root name
age: 66
---
spring:
config:
import:
- file:f:/pack-akf.yml</code>The imported file can override existing properties.
2.5 Importing Files Without Extension
Use bracket notation to tell Spring Boot the file’s format, e.g. file:f:/ack[.yml] imports a file without an extension as YAML.
<code>pack:
email: [email protected]</code>Injecting the value:
<code>@Value("${pack.email}")
private String email;</code>Result: email = [email protected]
All the above techniques allow fine‑grained control over where and how Spring Boot loads configuration data.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.