DEV Community

Cover image for SpringBoot Environment Variable
DrSimple
DrSimple

Posted on

SpringBoot Environment Variable

🌱 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} 
Enter fullscreen mode Exit fullscreen mode
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 
Enter fullscreen mode Exit fullscreen mode
βœ… Windows (CMD)
set PORT=8081 set DB_URL=jdbc:mysql://localhost:3306/mydb set DB_USER=root set DB_PASSWORD=secret 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Add dependency to pom.xml:

<dependency> <groupId>io.github.cdimascio</groupId> <artifactId>dotenv-java</artifactId> <version>3.0.0</version> </dependency> 
Enter fullscreen mode Exit fullscreen mode

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); } } 
Enter fullscreen mode Exit fullscreen mode

πŸ” 4. Accessing Environment Variables in Java Code
Spring allows you to inject values using @Value:

@Value("${DB_USER}") private String dbUser; 
Enter fullscreen mode Exit fullscreen mode

Or use Environment:

@Autowired private Environment env; public void printEnv() { System.out.println(env.getProperty("DB_USER")); } 
Enter fullscreen mode Exit fullscreen mode
🎯 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 
Enter fullscreen mode Exit fullscreen mode

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} 
Enter fullscreen mode Exit fullscreen mode

βœ… 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.

Image description

Top comments (0)