How to override application.properties during production in Spring-Boot?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In a Spring Boot application, application.properties or application.yml files are typically used for configuring various settings. When you move from development to production, you often need to override certain properties without modifying the actual configuration file. Spring Boot provides multiple strategies to override these configurations effectively. This article discusses various methods to override application.properties during production.
Understanding Spring Boot’s Configuration Hierarchy
Spring Boot follows a specific order when it comes to resolving properties. This order allows you to override the default settings through different means. Here are the key sources in order of precedence:
- DevTools global settings properties in the
$HOME/.config/spring-bootdirectory. - Test-specific
@TestPropertySourceannotations. - Properties files provided externally with
-Dspring.config.additional-locationor equvalent. - Command-line arguments (
--property=value). - Java System properties (
-Dproperty=value). - Environment variables.
- Application properties packaged inside your jar (
application.propertiesorapplication.yml). - Default properties (set using
SpringApplication.setDefaultProperties).
This layering allows developers to provide different property sources at runtime, making it possible to apply different configurations without changing or repackaging applications.
Techniques to Override Application Properties in Production
1. Environment-Specific Profile
Spring Boot supports profiles that allow you to define a different set of configurations for various environments (development, testing, production). You can create specific property files for each profile, such as application-prod.properties.
Example:
When you activate the prod profile, Spring Boot automatically picks up the properties defined in application-prod.properties.
2. External Configuration Files
You can place a configuration file outside the jar package and point to it using the spring.config.location property.
Example:
3. Command-Line Arguments
Pass properties as arguments when starting your application using the -- prefix.
Example:
4. System Environment Variables
Environment variables can override the default properties. These variables should follow a specific naming convention: keys in uppercase with dots replaced by underscores.
Example:
For a property like spring.datasource.url, the corresponding environment variable would be:
5. System Properties
Java system properties can be used to override configuration keys using the -D option in the startup command.
Example:
6. Spring Cloud Config
For more extensive configurations or microservices, Spring Cloud Config provides centralized management of configuration across multiple applications and environments using a central repository.
Key Differences and Notes
| Method | Priority Order | Use Cases | Notes |
| Environment-Specific Files | Low | Environment desired files | Needs a profile switch |
| External Configuration | High | Cluster specific settings | Location must be hardcoded or passed |
| Command-Line Arguments | Highest | Quick temporary overrides | Suitable for temporary/debugging purposes |
| Environment Variables | Medium | Environment general configs | Requires host setup |
| System Properties | High | Non-profiled overrides | Java specific, suitable for JVM-wide settings |
| Spring Cloud Config | Customizable | Centralized management | Additional setup required |
Summary
Spring Boot provides a flexible system for property management, with multiple ways to override default settings. Each method has its specific use case and advantages, fitting different stages of application setups. Combining these techniques allows one to create a robust and adaptable configuration strategy, reducing manual interventions and minimizing the risks during production deployments.
By applying these methods, you can build a seamless and efficient way to manage your application's configurations across different environments, ensuring that your Spring Boot applications behave as expected.
Related reading
- How to parse a JSON string into JsonNode in Jackson?
- How to parse JSON in Java
- How to pass a function as a parameter in Java?
- How to pass a MapString, String with application.properties
- How to pass an ArrayList to a varargs method parameter?
- How to pass JVM options from bootRun
- How to pass multiple bootstrap servers for listener using spring-kafka
- How to pass system property to Gradle task

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.