Spring overriding one application.property from command line
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Spring Boot is a popular framework for building standalone, production-grade Spring-based applications. One of its many powerful features is convention over configuration, which simplifies application setup. However, there are times when you need to override default settings or externalize configurations, such as using different settings for development and production environments. Spring Boot accommodates this need by allowing developers to override configuration values in several ways, including through the command line. This article dives into how to override a Spring Boot application's application.properties from the command line.
Spring Boot Configuration Hierarchy
Spring Boot has a well-defined configuration hierarchy to determine which property files and values take precedence. The order of precedence for configuration values is as follows:
- Command Line Arguments: Highest precedence, useful for overriding properties at runtime.
- Java System Properties: These can be set using the JVM
-Doptions. - OS Environment Variables: Environment variables available in operating systems.
- Application Properties & Yaml Files: Properties defined in
application.propertiesorapplication.ymlfiles. - Default Properties: Properties provided directly in code via Spring's
@PropertySource.
Understanding this hierarchy is essential because properties defined at a higher precedence level will override those at a lower level.
Overriding Properties from the Command Line
The simplest and most common method to override a property in Spring Boot is by using command line arguments. Command line arguments are passed directly to the java -jar command when running a Spring Boot application and have the highest precedence in Spring's property hierarchy.
Example
Assume you have the following properties defined in your application.properties file:
To override these properties when starting your application, you can specify them as command line arguments:
This command will override the server.port to 9090 and the spring.datasource.url to a different database.
Syntax
The syntax for overriding a property in the command line is:
Each property is prefixed by --, followed by the property key and its value.
Practical Scenarios
- Environment-Specific Configurations: While developing, you might want to use a different database, logging level, or port. Command line arguments can help switch between configurations seamlessly.
- Docker and CI/CD Pipelines: Use command line arguments to inject environment-specific variables that change between testing, staging, and production environments.
- Single Dependency Management: By changing property values directly from the command line, you can avoid maintaining multiple properties files for different environments.
Common Pitfalls
- Debatable Readability: Overusing command line arguments can obscure what properties are being set, making it difficult to track application behavior just by looking at the properties file.
- Shell Escape: Some values may need escaping or quoting, especially if they include special characters. For example:
- Complex Properties: Configuring complex properties like lists or maps using the command line can be tricky and might necessitate use of additional tools or scripts.
Summary Table
| Property Source | Usage Example | Description |
| Command Line Arguments | --property.key=value | Highest precedence. Overridden by command line during runtime start. |
| Java System Properties | -Dproperty.key=value | Set during JVM startup. Precedes OS and application properties. |
| OS Environment Variables | export PROPERTY_KEY=value | Environment variables set at OS level. Used widely in CI/CD setups. |
| Application Properties File | Edit application.properties | Standard way to configure applications. Located in the src/main/resources/ path. |
| Default Properties | @PropertySource in code | Lowest precedence. Useful for sensible defaults hardcoded in application. |
Conclusion
Overriding properties in Spring Boot from the command line is a quick and effective way to make dynamic changes to application configurations without changing code or property files. Especially crucial in production environments, this approach offers the flexibility to modify runtime behavior swiftly. By understanding Spring Boot's configuration hierarchy, developers can efficiently manage configuration changes and make robust, adaptable applications.
Related reading
- Spring PropertySource using YAML
- Spring Prototype scope and implementation specific properties
- Spring RabbitMQ - using manual channel acknowledgement on a service with RabbitListener configuration
- Spring RabbitTemplate - How to create queues automatically upon send
- Spring reactor executing consumers asynchronously
- Spring ResponseStatusException does not return reason
- Spring rest controller not returning html
- Spring RestTemplate - how to enable full debugging/logging of requests/responses?

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.