Spring Boot application.yml to ENV Conversion
Convert Spring Boot application.yml configuration to environment variables using Spring's relaxed binding rules. Learn the dot-notation and uppercase conventions.
Detailed Explanation
Spring Boot has a unique relationship between YAML configuration and environment variables. Its "relaxed binding" feature allows environment variables to override YAML properties following specific naming conventions.
Spring Boot application.yml:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: dbuser
password: dbpass
hikari:
maximum-pool-size: 10
minimum-idle: 5
connection-timeout: 30000
jpa:
hibernate:
ddl-auto: validate
show-sql: false
redis:
host: localhost
port: 6379
server:
port: 8080
servlet:
context-path: /api
logging:
level:
root: INFO
com.example.myapp: DEBUG
org.hibernate.SQL: DEBUG
Equivalent environment variables:
SPRING_DATASOURCE_URL=jdbc:postgresql://localhost:5432/mydb
SPRING_DATASOURCE_USERNAME=dbuser
SPRING_DATASOURCE_PASSWORD=dbpass
SPRING_DATASOURCE_HIKARI_MAXIMUM_POOL_SIZE=10
SPRING_DATASOURCE_HIKARI_MINIMUM_IDLE=5
SPRING_DATASOURCE_HIKARI_CONNECTION_TIMEOUT=30000
SPRING_JPA_HIBERNATE_DDL_AUTO=validate
SPRING_JPA_SHOW_SQL=false
SPRING_REDIS_HOST=localhost
SPRING_REDIS_PORT=6379
SERVER_PORT=8080
SERVER_SERVLET_CONTEXT_PATH=/api
LOGGING_LEVEL_ROOT=INFO
LOGGING_LEVEL_COM_EXAMPLE_MYAPP=DEBUG
LOGGING_LEVEL_ORG_HIBERNATE_SQL=DEBUG
Spring Boot's relaxed binding rules:
- Dots become underscores.
spring.datasource.url->SPRING_DATASOURCE_URL - Hyphens become underscores.
maximum-pool-size->MAXIMUM_POOL_SIZE - Everything is uppercased.
spring->SPRING - Nested keys are joined with underscores. Each YAML nesting level adds an underscore separator.
Special handling for lists:
spring:
profiles:
active:
- dev
- debug
SPRING_PROFILES_ACTIVE=dev,debug
# or with indexed notation:
SPRING_PROFILES_ACTIVE_0=dev
SPRING_PROFILES_ACTIVE_1=debug
Map keys with dots (logging levels):
The logging.level section maps package names to levels. The dots in package names (com.example.myapp) become underscores, which can create ambiguity. Spring resolves this by checking the full property path.
Common gotchas:
ddl-auto(with hyphen) andddlAuto(camelCase) both map toDDL_AUTOin ENV- Boolean values in YAML (
show-sql: false) become strings in ENV (SHOW_SQL=false) - Context paths with slashes (
/api) should not be quoted differently between formats
Spring Boot's relaxed binding makes YAML-to-ENV conversion reliable, but the reverse (ENV-to-YAML) requires knowledge of the original YAML structure to reconstruct proper nesting.
Use Case
Deploying a Spring Boot application to a cloud platform (AWS ECS, GCP Cloud Run) where configuration is injected via environment variables, converting the local application.yml to ENV vars for the container runtime.