Spring Boot
environment variables
application properties
configuration
Java

Using env variable in Spring Boot's application.properties

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Spring Boot applications typically use the application.properties or application.yml files to define configurations. While these files are suitable for defining static configurations, there are instances where dynamic configuration, driven by environmental variables, becomes necessary. This approach enhances flexibility, particularly in cloud deployments or environments with multiple configurations like development, testing, and production.

In this article, we will explore how to use environment variables in Spring Boot's application.properties file. We will delve into technical explanations, illustrate with examples, and conclude with key takeaways.

Understanding Application Properties

Spring Boot utilizes the application.properties or application.yml files to configure the application. These configurations can be anything from database settings to server ports. By design, Spring Boot offers the flexibility to externalize these configurations using environment variables. This approach aligns well with the 12-factor app methodology, where configurations are stored in the environment.

Environment Variables in application.properties

In Spring Boot, you can leverage environment variables directly in the application.properties file. Environment variables can be injected via command-line arguments, operating system variables, or build tools like Maven or Gradle. Below, we illustrate these approaches with examples.

Syntax for Using Environment Variables

To reference an environment variable within application.properties, use the following syntax:

properties
property.name=${ENV_VARIABLE_NAME:defaultValue}
  • ENV_VARIABLE_NAME is the name of the environment variable.
  • defaultValue is optional and will be used if the environment variable is not found.

Example

Consider an application that needs to use different databases for development and production environments. Here is how you can use environment variables with application.properties:

properties
spring.datasource.url=${DATABASE_URL:jdbc:mysql://localhost:3306/mydb}
spring.datasource.username=${DATABASE_USER:root}
spring.datasource.password=${DATABASE_PASS:password}

In this example, DATABASE_URL, DATABASE_USER, and DATABASE_PASS are environment variables. If these are not set, the application will use the default values specified.

Setting Environment Variables

Environment variables can be set directly in the operating system or via command-line arguments:

System Environment

On Unix-like systems, you can set environment variables using export:

bash
export DATABASE_URL=jdbc:mysql://production-db:3306/proddb
export DATABASE_USER=admin
export DATABASE_PASS=securePassword

On Windows, environment variables can be set using the set command in CMD or PowerShell.

powershell
set DATABASE_URL=jdbc:mysql://production-db:3306/proddb
set DATABASE_USER=admin
set DATABASE_PASS=securePassword

Command-line Argument

Environment variables can also be provided as command-line arguments:

bash
java -jar myapp.jar --DATABASE_URL="jdbc:mysql://production-db:3306/proddb" --DATABASE_USER=admin --DATABASE_PASS=securePassword

Benefits of Using Environment Variables

  • Flexibility: Easily switch configurations without code changes.
  • Security: Separate sensitive information like passwords from source code.
  • Consistency: Ensure uniform configuration across different environments (dev, test, prod).
  • Immutability: As external values, environment variables can contribute to immutable infrastructure practices.

Additional Topics

Profile-specific Properties

Spring Boot enables profile-specific configurations, i.e., you can have application-dev.properties and application-prod.properties. The active profile can be determined through environment variables:

bash
spring.profiles.active=${APP_PROFILE:dev}

The above configuration allows setting the APP_PROFILE environment variable to switch between different profiles dynamically.

Using YAML for Advanced Configurations

If you're using YAML (application.yml), environment variables can be referenced similarly:

yaml
1spring:
2  datasource:
3    url: ${DATABASE_URL:jdbc:mysql://localhost:3306/mydb}
4    username: ${DATABASE_USER:root}
5    password: ${DATABASE_PASS:password}

Environment Variable Precedence

Spring Boot follows a well-defined precedence order for configuration properties:

  1. Command-line arguments
  2. OS environment variables
  3. application.properties/application.yml
  4. Profile-specific properties (e.g., application-dev.properties)

Summary Table

PropertyDescriptionExampleDefault Value
spring.datasource.urlDatabase connection URL${DATABASE_URL}jdbc:mysql://localhost:3306/mydb
spring.datasource.usernameDatabase username${DATABASE_USER}root
spring.datasource.passwordDatabase password${DATABASE_PASS}password
spring.profiles.activeActive Spring profile${APP_PROFILE}dev

Conclusion

Using environment variables in Spring Boot's application.properties is a powerful practice to ensure flexible, secure, and consistent application configurations. By externalizing your configuration properties, you can efficiently manage settings across various environments and adapt your applications more fluidly to changes in deployment infrastructure. Understanding how to implement and leverage this capability is crucial for developers working on scalable and maintainable Spring Boot applications.


Course illustration
Course illustration

All Rights Reserved.