In Spring Boot, the property files are commonly used to externalize application configuration. It allows us to have a central location for managing the explicit behavior of the application in different environments. Property files can contain key-value pairs that configure various aspects of the application, such as database connection details, logging settings, or application-specific parameters.
In this tutorial, we’ll explore different ways to load property files in Spring Boot.
1. Loading Property Files in Spring Boot
Spring Boot supports getting properties from numerous locations. For a normal application, the following resources are taken into consideration in a given order:
- application.properties/application.yaml outside of the packaged application
- application.properties/application.yaml packaged inside the application
- OS environment variables
- Java system properties
- Command-line arguments
Instead of .properties files, we could also use a YAML file (such as application.yaml) to express the configuration properties. The same rules as with .properties files still apply.
When Spring profiles are used, the application can also load a profile-specific properties file based on the active profiles. The profiles to activate can be passed through the ‘spring.profiles.active‘ property. The profile-specific ‘application-{profile}.properties‘ profiles take precedence over the non-profile-specific ones.
- application-{profile}.properties outside the packaged application
- application.properties outside of the packaged application
- application-{profile}.properties packaged inside the application
- application.properties packaged inside the application
- OS environment variables
- Java system properties
- Command-line arguments
2. Loading Property Files from Outside the Packaged Application
By default, Spring Boot looks for the application.properties file in the ‘src/main/resources
‘ directory of the project. However, we can specify an external location for the application.properties file using the ‘spring.config.location
‘ property.
In this example, Spring Boot loads the application.properties file from the specified external location (/path/to/external/application.properties
) instead of the default location.
java -jar myapp.jar --spring.config.location=/path/to/external/application.properties
Alternatively, we can include the application.properties
file in a directory that is included in the classpath. This allows us to package the application.properties
file with the application but still externalize it for configuration purposes.
In this example, Spring Boot looks for the application.properties
file in the config
directory located on the classpath. We can include the config
directory inside the packaged JAR file or in a separate directory outside the JAR file.
java -jar myapp.jar --spring.config.additional-location=classpath:/config/
application.properties
3. Loading Properties from Default By default, Spring Boot looks for properties in the application.properties
or application.yml
file located in the src/main/resources
directory. We can define properties directly in these files using the key-value format.
# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=myuser
spring.datasource.password=mypassword
Spring Boot automatically loads properties from these files and makes them available to the application.
4. Loading Profile-specific Property files
Additionally, we can use profile-specific application.properties
files to customize application behavior for different environments or deployment scenarios. Profile-specific properties files are named application-{profile}.properties
, where {profile}
is the name of the profile.
In this example, Spring Boot activates the production
profile and loads properties from application-production.properties
.
java -jar myapp.jar --spring.profiles.active=production
5. Loading Properties from Custom Property Files
We can also load properties from custom property files by specifying their locations using the @PropertySource
annotation. This allows separating the configuration into multiple files or load properties from external locations.
@Configuration
@PropertySource("classpath:custom.properties")
public class CustomConfiguration {
@Value("${custom.property}")
private String customProperty;
// Bean logic here
}
6. Passing / Overriding a Property from Environment Variables
If we are considering only one or two properties, Spring Boot allows us to override properties using environment variables. Environment variables provide a way to configure the application without modifying property files, making it easier to manage configuration in different environments.
In this example, the spring.datasource.url
property is overridden by the SPRING_DATASOURCE_URL
environment variable.
export SPRING_DATASOURCE_URL=jdbc:mysql://localhost:3306/mydb
7. Passing / Overriding a Property from System Properties
Similar to environment variables, we can override properties using system properties. System properties are set using JVM command-line options (-Dproperty=value
) or programmatically in the application.
java -jar myapp.jar -Dspring.datasource.url=jdbc:mysql://localhost:3306/mydb
Note that System properties take precedence over properties defined in property files or environment variables.
8. Conclusion
Spring Boot provides various ways to load property files and configure applications. By leveraging application.properties
, application.yml
, custom property files, environment variables, and system properties, we can externalize configuration and customize the application behavior without changing the code.
Choose the approach that best fits our requirements and development practices to effectively manage configuration in our Spring Boot applications.
Happy Learning !!
Comments