Master Spring Boot 3: Real‑World Config File Tricks and Advanced Use Cases
This guide walks through Spring Boot 3's externalized configuration system, showing how to locate, rename, and override property files, use optional and wildcard locations, import additional resources, handle missing files, and apply profile‑specific settings with concrete command‑line examples and code snippets.
1. Introduction
Spring Boot supports flexible externalized configuration via application.properties or application.yml. It can load multiple files from the classpath or the current directory, merge their contents, and prioritize values based on location order, making it easy to switch between development, testing, and production environments.
2. Practical Scenarios
2.1 Default configuration loading order
When the application starts, Spring Boot searches for configuration files in the following order:
Classpath root
Classpath /config package
Current directory
Current directory /config sub‑directory
Sub‑directories of /config Values from later entries override earlier ones, and the files are merged rather than exclusive.
2.2 Changing the default file name
The default name application can be changed with the spring.config.name environment property, e.g.:
java -jar springboot-configfile-1.0.0.jar \
--spring.config.name=pack2.3 Modifying the search locations
Use spring.config.location to specify explicit locations (comma‑separated). The optional: prefix prevents startup failure if a file is missing.
java -jar springboot-configfile-1.0.0.jar \
--spring.config.location=optional:classpath:/pack.yml,optional:file:f:/2.4 Adding extra configuration files
Additional files can be loaded without replacing the defaults via spring.config.additional-location. Their properties override those from earlier locations.
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.yml2.5 Optional locations and wildcard support
Appending optional: allows non‑existent paths to be ignored. Wildcards (e.g., config/*/) expand to all matching sub‑directories, enabling batch loading of related configuration sets such as Redis and MySQL files.
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=ignore2.6 Profile‑specific files
When a profile (e.g., prod or live) is active, Spring Boot also loads application‑{profile}.yml. Later profiles override earlier ones.
spring:
profiles:
active:
- prod
- liveWith spring.config.name=pack, pack‑prod.yml and pack‑live.yml are loaded, and the live values win.
2.7 Importing other resources
The spring.config.import property can pull in extra files (including those without extensions) at any point in the configuration hierarchy. Extension hints are supplied in brackets when importing extension‑less files.
spring:
config:
import:
- file:f:/ack[.yml]2.8 Handling missing files gracefully
Set spring.config.on-not-found=ignore to continue startup even if a configured location does not exist.
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:/pack-akf1.yml \
--spring.config.on-not-found=ignore3. Code Examples
Injecting values from merged configuration files:
@Value("${pack.name}")
private String name;
@Value("${pack.age}")
private Integer age;Reading profile‑specific properties:
@Value("${redis.name:}")
private String redisName;
@Value("${mysql.name:}")
private String mysqlName;4. Summary
The article demonstrates how Spring Boot 3’s configuration mechanism can be customized extensively: rename the base file, relocate it, add optional and wildcard locations, import additional resources, and manage profile‑specific overrides, all while ensuring the application starts reliably even when some files are absent.
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.
