Spring Boot
application.properties
production environment
configuration management
override settings

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.

Browse interview questions

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:

  1. DevTools global settings properties in the $HOME/.config/spring-boot directory.
  2. Test-specific @TestPropertySource annotations.
  3. Properties files provided externally with -Dspring.config.additional-location or equvalent.
  4. Command-line arguments (--property=value).
  5. Java System properties (-Dproperty=value).
  6. Environment variables.
  7. Application properties packaged inside your jar (application.properties or application.yml).
  8. 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:

properties
1// application.properties
2spring.profiles.active=prod
3
4// application-prod.properties
5server.port=8081
6logging.level.root=WARN

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:

bash
java -jar myapp.jar --spring.config.location=/path/to/production/application.properties

3. Command-Line Arguments

Pass properties as arguments when starting your application using the -- prefix.

Example:

bash
java -jar myapp.jar --server.port=8081 --logging.level.root=WARN

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:

bash
export SPRING_DATASOURCE_URL=jdbc:postgresql://prod-db-url:5432/prod

5. System Properties

Java system properties can be used to override configuration keys using the -D option in the startup command.

Example:

bash
java -Dserver.port=8081 -jar myapp.jar

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

MethodPriority OrderUse CasesNotes
Environment-Specific FilesLowEnvironment desired filesNeeds a profile switch
External ConfigurationHighCluster specific settingsLocation must be hardcoded or passed
Command-Line ArgumentsHighestQuick temporary overridesSuitable for temporary/debugging purposes
Environment VariablesMediumEnvironment general configsRequires host setup
System PropertiesHighNon-profiled overridesJava specific, suitable for JVM-wide settings
Spring Cloud ConfigCustomizableCentralized managementAdditional 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
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.