How can I override Spring Boot application.properties programmatically?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Spring Boot applications, it's common to manage configuration properties using the application.properties or application.yml files. However, there are times when you might need to override these properties programmatically, perhaps in response to different environments, user inputs, or specific runtime conditions. This article explores various ways to achieve this.
Understanding Spring Boot's Configuration Properties
Spring Boot's configuration properties provide a powerful mechanism for controlling the application's behavior. These properties are loaded at startup and can be sourced from:
application.propertiesorapplication.ymlfiles.- Environment variables.
- Command-line arguments.
- Java system properties.
- Dedicated configuration classes.
The order of precedence determines which source can override which, and the programmatic approach typically sits atop this hierarchy.
Programmatically Overriding Properties
Method 1: Using SpringApplication
One of the most straightforward ways to override properties programmatically is through SpringApplication. This can be achieved by calling methods on the SpringApplication instance before the application starts.
Method 2: Customizing ApplicationContextInitializer
Another approach involves creating a custom ApplicationContextInitializer.
To integrate this initializer, you need to configure your SpringApplication:
Method 3: Using EnvironmentPostProcessor
The EnvironmentPostProcessor is a more advanced option that allows you to post-process the Environment before the application context context is refreshed.
This class should be registered in a META-INF/spring.factories file.
Summary Table
| Method | Class Used | Key Features | Context of Use |
| Spring Application Default Properties | SpringApplication | Simple and direct | Basic overrides at startup |
| Custom Initializer | ApplicationContextInitializer | More control with ApplicationContext | For initialization-specific properties |
| Post Processor | EnvironmentPostProcessor | Advanced, operates before context refresh | For complex, environment-specific overrides |
Additional Details and Considerations
- Profiles: Remember that you can use profiles to handle different configurations for different environments.
- Hierarchy: Understand the source precedence in Spring Boot. Programmatically setting properties will often override properties from lower-precedence sources like
application.properties. - Security: Be cautious when overriding sensitive properties like database credentials. Ensure security measures are in place.
By selecting the appropriate method based on your specific needs, you can dynamically adjust the configuration of your Spring Boot application, tailoring it to different contexts and enhancing its adaptability.
Related reading
- How can I pad a String in Java?
- How can I parse/format dates with LocalDateTime? (Java 8)
- How can I pass a parameter to a Java Thread?
- How can I play sound in Java?
- How can I produce messages with Kafka 8.2 API in Java?
- How can I propagate exceptions between threads?
- How can I properly compare two Integers in Java?
- How can I provide different database configurations with Spring Boot?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.