How do you use environment variables in Spring Boot?
Table of Contents
- Introduction
- Steps to Use Environment Variables in Spring Boot
- 1. Using Environment Variables in
**application.properties**
or**application.yml**
- 2. Setting Environment Variables
- 3. Using Environment Variables for Profiles
- 4. Accessing Environment Variables in Code
- 5. Using
**@Value**
Annotation to Inject Environment Variables - 6. Overriding Configuration in
**application.properties**
with Command-Line Arguments - 7. Using Default Values for Missing Environment Variables
- 1. Using Environment Variables in
- Practical Example
- Conclusion
Introduction
Environment variables play a crucial role in Spring Boot applications, allowing you to externalize configuration and manage different environments (e.g., development, production). By using environment variables, you can keep sensitive information (like API keys and database credentials) outside the codebase and make your application more flexible, portable, and secure.
This guide will explain how to use environment variables in Spring Boot, how they integrate with application configuration, and how to configure Spring Boot to read and use these variables effectively.
Steps to Use Environment Variables in Spring Boot
1. Using Environment Variables in **application.properties**
or **application.yml**
Spring Boot provides a seamless way to inject environment variables into the application.properties
or application.yml
configuration files. You can reference environment variables using the ${}
syntax.
Example in application.properties
:
Example in application.yml
:
In these examples, the values for spring.datasource.url
, spring.datasource.username
, and spring.datasource.password
will be taken from environment variables DB_URL
, DB_USERNAME
, and DB_PASSWORD
, respectively.
2. Setting Environment Variables
Environment variables can be set in different ways, depending on your operating system or deployment method. Below are examples of setting environment variables.
On Linux/macOS:
On Windows:
In Docker:
If you are running your Spring Boot application in a Docker container, you can pass environment variables through the Docker command:
3. Using Environment Variables for Profiles
You can also use environment variables to control which Spring Boot profile is active. This allows you to specify different configurations for development, testing, or production environments.
For example, in your application.properties
file, you might specify:
Then, you can set the SPRING_PROFILE
environment variable to either dev
, prod
, or test
based on the environment:
This will activate the application-prod.properties
configuration file, and Spring Boot will load properties specific to the production environment.
4. Accessing Environment Variables in Code
In addition to referencing environment variables in the application.properties
or application.yml
, you can access environment variables directly in your Spring Boot Java code using the Environment
object.
Example of Accessing Environment Variables Programmatically:
In this example, the Environment
object is used to retrieve the values of the DB_URL
, DB_USERNAME
, and DB_PASSWORD
environment variables and print them.
5. Using **@Value**
Annotation to Inject Environment Variables
Spring Boot also allows you to inject environment variables directly into Spring-managed beans using the @Value
annotation.
Example:
In this example, the @Value
annotation is used to inject the values of the DB_URL
, DB_USERNAME
, and DB_PASSWORD
environment variables directly into the DatabaseConfig
class.
6. Overriding Configuration in **application.properties**
with Command-Line Arguments
Spring Boot also supports overriding configuration values with command-line arguments, which can be useful when deploying or running your application in different environments.
For example, to override the DB_URL
configuration, you can pass it as a command-line argument:
This will override the spring.datasource.url
property defined in your application.properties
or application.yml
file.
7. Using Default Values for Missing Environment Variables
In case an environment variable is not set, you can provide default values directly in your configuration files by using the :defaultValue
syntax.
Example:
In this example, if the DB_URL
environment variable is not set, Spring Boot will default to jdbc:mysql://localhost:3306/defaultdb
.
Practical Example
Let’s combine everything and create a practical example for configuring a Spring Boot application to use environment variables.
application.properties
:
Setting Environment Variables:
Running the Application:
In this example:
- The
DB_URL
,DB_USERNAME
, andDB_PASSWORD
environment variables are read to configure the datasource. - The active Spring profile (
prod
) is determined by theSPRING_PROFILE
environment variable, activating theapplication-prod.properties
file.
Conclusion
Using environment variables in Spring Boot enhances the flexibility and portability of your applications. It helps externalize sensitive configuration, manage different environments, and integrate with cloud-native environments. By referencing environment variables in application.properties
or application.yml
, accessing them programmatically through the Environment
object, and using the @Value
annotation, you can easily configure and manage application settings in a Spring Boot application. This approach reduces the need for hardcoded values, improves security, and ensures smooth deployment across various environments.