Read environment variable in SpringBoot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Spring Boot is a popular framework for building Java-based applications, providing a comprehensive set of tools to quickly set up robust applications with minimal configuration. One of the key features of Spring Boot is its ability to easily read environment variables. Environment variables are part of the operating system’s shell environment and provide a way to store configuration settings outside of the application, allowing for more flexible and scalable application deployments.
Why Use Environment Variables?
Environment variables serve several purposes in software development and deployment:
- Configuration Management: They simplify the management of configuration settings for different environments (development, staging, production).
- Security: Sensitive information, such as API keys or database credentials, can be stored as environment variables, keeping them separate from the codebase.
- Portability: Applications can be easily moved between different environments without changing the code.
Reading Environment Variables in Spring Boot
Spring Boot provides multiple ways to read environment variables, making it convenient to configure applications dynamically.
Using @Value Annotation
The @Value annotation is a powerful tool in Spring Boot for injecting values into fields and methods. You can use it to read environment variables as follows:
In this example, MY_ENV_VAR is the name of the environment variable. If it's not set, default_value will be used as a fallback.
Using Environment Interface
The Environment interface allows you to access environment properties programmatically:
Using @ConfigurationProperties
@ConfigurationProperties is another way to bind environment variables to a Java class:
With this setup, you can access the environment variable using myapp.env-var.
Configuring Environment Variables
Environment variables can be set in various ways, depending on the operating system and development environment. Here are some examples:
Linux/MacOS
You can set an environment variable for a session using:
Windows
To set an environment variable in a command prompt window:
Docker
When running Spring Boot applications inside Docker containers, you can specify environment variables in the Dockerfile or with the docker run command:
Table: Key Points for Reading Environment Variables in Spring Boot
| Method | Description | Example Usage |
@Value Annotation | Injects the environment variable directly into a field | @Value("${MY_ENV_VAR:default_value}") |
Environment Interface | Programmatically access environment variables | env.getProperty("MY_ENV_VAR", "default_value") |
@ConfigurationProperties | Binds environment variables to a POJO-like structure | @ConfigurationProperties(prefix = "myapp") |
| Terminal/Command-line | Set environment variables manually for different OS | Linux: export VAR=value
Windows: set VAR=value |
| Docker | Manage environment variables in containerized applications | ENV VAR=value in Dockerfile
or -e VAR=value in docker run command |
Best Practices
- Use Default Values: Always provide a default value when accessing environment variables to ensure application stability.
- Centralized Configuration: Use centralized configuration management tools like Spring Cloud Config to manage environment variables for large deployments.
- Secure Sensitive Data: Use environment variables for sensitive information to minimize exposure in the codebase.
By reading environment variables, your Spring Boot application can become more flexible and adaptive to different deployment environments, enhancing maintainability and scalability.
Related reading
- Read file from resources folder in Spring Boot
- Read file from resources folder in Spring Boot
- Read resource text file to String in Java
- Read URL to String in few lines of Java code
- Reading a List from properties file and load with Spring annotation @Value
- Reading a plain text file in Java
- Reading a resource file from within jar
- Read/Write String from/to a File in Android

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.