7 Ways to Load Property Files in Spring Boot 3.2.5
This article explains seven practical methods for loading property files in Spring Boot, covering default locations, external files, classpath resources, profile‑specific files, custom @PropertySource usage, and overriding via environment variables or JVM system properties.
1. Introduction
In Spring Boot, property files externalize configuration, allowing centralized management of settings such as database connections, logging, or custom parameters across environments.
This article explores seven methods to load property files in Spring Boot.
2. Practical Cases
2.1 Loading property files in Spring Boot
Spring Boot can obtain properties from multiple locations in the following order:
External application.properties / application.yml
Internal application.properties / application.yml
Operating system environment variables
Java system properties
Command‑line arguments
When a profile is active, Spring Boot also loads profile‑specific files such as application-{profile}.properties , which have higher precedence.
2.2 Loading external property files
By default Spring Boot searches src/main/resources for application.properties . You can override the location with the spring.config.location property, e.g.
<code>java -jar myapp.jar --spring.config.location=/home/app/application.properties</code>Alternatively, place the file in a classpath directory (e.g., config/ ) and use spring.config.additional-location=classpath:/config/ .
2.3 Loading the default application.properties
Spring Boot automatically loads application.properties or application.yml from src/main/resources . Example entries:
<code>spring.datasource.url=jdbc:mysql://localhost/mydb
spring.datasource.username=root
spring.datasource.password=xxxooo</code>2.4 Loading profile‑specific property files
Files named application-{profile}.properties are used when the profile is activated, e.g.
<code>java -jar myapp.jar --spring.profiles.active=prod</code>This loads properties from application-prod.properties .
2.5 Loading custom property files
Use @PropertySource to import additional files:
<code>@Configuration
@PropertySource("classpath:pack.properties")
public class CustomConfiguration {
@Value("${pack.app.title}")
private String title;
}</code>Multiple files can be loaded with @PropertySources .
2.6 Overriding properties with environment variables
Set an environment variable to replace a property, e.g.
<code>export SPRING_DATASOURCE_URL=jdbc:mysql://localhost/mydb</code>2.7 Overriding properties with system properties
Use JVM arguments to set system properties, which have higher precedence than environment variables:
<code>java -jar myapp.jar -Dspring.datasource.url=jdbc:mysql://localhost/mydb</code>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.