Spring
application.properties
command line
configuration
override

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.

Browse interview questions

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:

  1. Command Line Arguments: Highest precedence, useful for overriding properties at runtime.
  2. Java System Properties: These can be set using the JVM -D options.
  3. OS Environment Variables: Environment variables available in operating systems.
  4. Application Properties & Yaml Files: Properties defined in application.properties or application.yml files.
  5. 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:

properties
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb

To override these properties when starting your application, you can specify them as command line arguments:

bash
$ java -jar my-application.jar --server.port=9090 --spring.datasource.url=jdbc:mysql://localhost:3306/otherdb

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:

bash
$ java -jar <your-jar-file>.jar --<property>=<value>

Each property is prefixed by --, followed by the property key and its value.

Practical Scenarios

  1. 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.
  2. Docker and CI/CD Pipelines: Use command line arguments to inject environment-specific variables that change between testing, staging, and production environments.
  3. Single Dependency Management: By changing property values directly from the command line, you can avoid maintaining multiple properties files for different environments.

Common Pitfalls

  1. 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.
  2. Shell Escape: Some values may need escaping or quoting, especially if they include special characters. For example:
bash
   $ java -jar app.jar --password="my\$ecureP@ssw0rd"
  1. 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 SourceUsage ExampleDescription
Command Line Arguments--property.key=valueHighest precedence. Overridden by command line during runtime start.
Java System Properties-Dproperty.key=valueSet during JVM startup. Precedes OS and application properties.
OS Environment Variablesexport PROPERTY_KEY=valueEnvironment variables set at OS level. Used widely in CI/CD setups.
Application Properties FileEdit application.propertiesStandard way to configure applications. Located in the src/main/resources/ path.
Default Properties@PropertySource in codeLowest 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
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track 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.

Browse interview questions

All Rights Reserved.