Master Spring Boot Configuration: Files, Profiles, and Property Injection
This guide explains how Spring Boot loads configuration files, compares properties and YAML formats, shows code examples for custom properties, XML imports, command‑line arguments, and profile‑specific settings, and details the underlying loading order and priority rules.
Configuration Files
Spring Boot creates an application.properties file under src/main/resources by default. A minimal example:
server.port=8080
server.servlet.context-path=/helloSpring Boot also supports YAML configuration via an application.yml file placed in the same resources directory. YAML offers object‑oriented syntax and is preferred for complex data structures.
server:
port: 8080
servlet:
context-path: /helloYAML requirements:
Case‑sensitive.
Indentation represents hierarchy; use spaces only (commonly 2 or 4).
If both application.properties and application.yml exist, Spring Boot first reads properties, then overrides with values from YAML when a key is missing in the properties file.
Configuration File Source Code Analysis
The class ConfigFileApplicationListener in the org.springframework.boot.context.config package defines the default loading behavior. Key constants include:
package org.springframework.boot.context.config;
public class ConfigFileApplicationListener implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
private static final String DEFAULT_PROPERTIES = "defaultProperties";
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
private static final String DEFAULT_NAMES = "application";
// ... other code omitted
}Spring Boot searches the following locations in order (lowest to highest priority):
./config/ (project root)
./ (project root)
classpath:/config/
classpath:/
Command‑Line and External Configuration
You can specify an external configuration directory with the command‑line option: --spring.config.location=/usr/local/config/ XML configuration can be imported when necessary using @ImportResource: @ImportResource("classpath:mq.xml") Direct command‑line arguments have the highest precedence, e.g.:
java -jar xx.jar --server.port=8080Custom Configuration Properties
Beyond the standard Spring Boot properties, you can define your own and inject them with @Value or bind them to a bean with @ConfigurationProperties.
Example of custom properties in application.properties:
# Custom properties
admin.name=Tom
admin.age=25
admin.phone=156********Injecting with @Value (providing a default value of Guest if the key is missing):
@Value("${admin.name:Guest}")
private String adminName;
@RequestMapping("/hello")
public String hello(){
return "Hello admin:" + adminName + "!";
}Binding a group of properties to a POJO using @ConfigurationProperties:
@Component
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "admin")
public class SystemAdmin {
private String name;
private int age;
// getters and setters omitted
}Loose binding allows different naming styles (camelCase, kebab‑case, snake_case) to map to the same fields.
Injecting the bound bean:
@Resource
private SystemAdmin systemAdmin;
@RequestMapping("/hello")
public String hello(){
return "Hello admin:" + systemAdmin.getName() + "!";
}Profile‑Specific Configuration
For different environments, Spring Boot loads files named application‑{profile}.properties (or .yml) such as:
development: application-dev.properties testing: application-test.properties production: application-prod.properties The active profile is set via the spring.profiles.active property in application.properties. For example, setting spring.profiles.active=dev loads application-dev.properties.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
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.
