How to Use Environment Variables in Spring Boot
In Spring Boot, you can use
environment variables by referencing them in your application.properties or application.yml files with the ${VAR_NAME} syntax. Spring Boot automatically reads these variables at runtime, allowing you to configure your app without hardcoding sensitive or environment-specific data.Syntax
Use environment variables in Spring Boot configuration files by enclosing the variable name in ${}. You can provide a default value using : if the environment variable is not set.
${VAR_NAME}: Reads the environment variableVAR_NAME.${VAR_NAME:defaultValue}: UsesdefaultValueifVAR_NAMEis not set.
properties
spring.datasource.url=${DB_URL:jdbc:h2:mem:testdb}
spring.datasource.username=${DB_USER:sa}
spring.datasource.password=${DB_PASS:password}Example
This example shows how to use environment variables to configure a database connection in Spring Boot. The app reads DB_URL, DB_USER, and DB_PASS from the environment or uses defaults if they are missing.
java
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @RestController class ConfigController { @Value("${DB_URL:jdbc:h2:mem:testdb}") private String dbUrl; @GetMapping("/dbinfo") public String getDbInfo() { return "Database URL: " + dbUrl; } }
Output
Database URL: jdbc:h2:mem:testdb
Common Pitfalls
- Not setting environment variables before running the app causes Spring Boot to use default values or fail if no default is provided.
- Using incorrect variable names or typos in
${VAR_NAME}will result in unresolved placeholder errors. - For complex types, environment variables are always strings; conversion must be handled by Spring Boot or manually.
- Remember environment variables are case-sensitive on some systems.
properties
spring.datasource.password=${DB_PASS}
# If DB_PASS is not set, this causes an error
# Correct way with default:
spring.datasource.password=${DB_PASS:defaultPass}Quick Reference
Tips for using environment variables in Spring Boot:
- Use
${VAR_NAME:default}to avoid errors if variables are missing. - Set environment variables in your OS or container before starting the app.
- Use
@Valueannotation to inject variables into Spring beans. - Check variable names carefully for typos and case sensitivity.
Key Takeaways
Use ${VAR_NAME} syntax in application.properties or application.yml to read environment variables.
Provide default values with ${VAR_NAME:default} to prevent startup errors.
Inject environment variables into Spring beans using @Value annotation.
Always set environment variables in your system or container before running the app.
Check for typos and case sensitivity to avoid unresolved placeholder errors.