π± How to Use Environment Variables in Spring Boot
Spring Boot makes it easy to load and use environment variables securely and cleanly. Whether you're building a local app or deploying to production, managing secrets and configs via environment variables is a best practice.
In this guide, you'll learn how to:
- Read environment variables in
application.properties
- Use
.env
files (locally) - Inject variables into Java code
- Use Spring profiles for different environments
π¦ 1. Using Environment Variables in application.properties
Spring Boot automatically maps environment variables to properties.
# src/main/resources/application.properties
server.port=${PORT:8080}
spring.datasource.url=${DB_URL}
spring.datasource.username=${DB_USER}
spring.datasource.password=${DB_PASSWORD}
Note:
${PORT:8080} β Uses the PORT environment variable or defaults to 8080.
${DB_URL} β Fails to start if DB_URL is not set.
π 2. Setting Environment Variables
β
Unix/macOS
export PORT=8081
export DB_URL=jdbc:mysql://localhost:3306/mydb
export DB_USER=root
export DB_PASSWORD=secret
β Windows (CMD)
set PORT=8081
set DB_URL=jdbc:mysql://localhost:3306/mydb
set DB_USER=root
set DB_PASSWORD=secret
You can also set these in Docker Compose, Kubernetes, or your cloud platform settings.
π 3. Using a .env File Locally (Optional)
Spring Boot doesn't load .env by default, but you can use dotenv-java or configure your IDE to do so.
Using dotenv-java:
# .env
PORT=8082
DB_URL=jdbc:mysql://localhost:3306/mydb
DB_USER=root
DB_PASSWORD=secret
Add dependency to pom.xml:
<dependency>
<groupId>io.github.cdimascio</groupId>
<artifactId>dotenv-java</artifactId>
<version>3.0.0</version>
</dependency>
Load in your main():
import io.github.cdimascio.dotenv.Dotenv;
@SpringBootApplication
public class MainApp {
public static void main(String[] args) {
Dotenv dotenv = Dotenv.load();
SpringApplication.run(MainApp.class, args);
}
}
π 4. Accessing Environment Variables in Java Code
Spring allows you to inject values using @Value:
@Value("${DB_USER}")
private String dbUser;
Or use Environment:
@Autowired
private Environment env;
public void printEnv() {
System.out.println(env.getProperty("DB_USER"));
}
π― 5. Use Spring Profiles for Environments
Create different config files for dev, test, and prod:
Set active profile
export SPRING_PROFILES_ACTIVE=dev
# application-dev.properties
logging.level.root=DEBUG
# application-prod.properties
logging.level.root=ERROR
Spring will automatically load the file that matches the active profile.
π¦ 6. Using Environment Variables in application.yml
If you use YAML instead of .properties:
server:
port: ${PORT:8080}
spring:
datasource:
url: ${DB_URL}
username: ${DB_USER}
password: ${DB_PASSWORD}
β
Summary
β
You can use environment variables in Spring Boot via:
${ENV_VAR} in application.properties or application.yml
@Value or Environment in code
Profiles for multiple environments
.env support with a helper library
π― Keep your apps secure, portable, and environment-agnostic by externalizing your configuration.
Top comments (0)