Environment Configuration
Twelve-factor Orders services configure via the environment, not committed secrets.
Introduction
Twelve-factor Orders services configure via the environment, not committed secrets. Boot 3 resolves ${DB_URL}, relaxed binding, and optional defaults from env vars, ConfigMaps, and secret mounts.
Locally you export variables or use a non-committed .env loader; in Kubernetes you inject ConfigMaps for non-secrets and Secrets/CSI for credentials; in CI you inject pipeline variables.
Fail fast when required values are missing. Prefer a secrets manager for rotation and audit over long-lived env blobs.
This lesson shows how TechLearningPRO Orders binds datasource and integration settings without putting passwords in git.
Purpose of this lesson
Configure Orders with env-driven properties, defaults, and startup validation — no secrets in source.
Understanding the topic
Environment variables override YAML and feed @ConfigurationProperties.
- placeholders:
${NAME:default}for optional,${NAME}for required. - relaxed binding:
DB_URLmaps todb.url/ nested properties. - priority: command line > env > profile YAML > base YAML.
- typed config:
@ConfigurationPropertieswith validation annotations. - secrets: mount or inject; never commit real values.
Step-by-step explanation
- Replace hard-coded JDBC and mail credentials with
${...}placeholders. - Create
OrdersDataSourceProperties(or use Spring Boot's datasource props) with validation. - Document required env vars in the service README for Ops.
- Start without
DB_PASSWORDand confirm Boot fails clearly.
Informative example
Env-backed Orders datasource and typed Stripe-free payment placeholder config.
# application.ymlspring:datasource:url: ${DB_URL:jdbc:postgresql://localhost:5432/orders}username: ${DB_USER:orders}password: ${DB_PASSWORD}data:redis:host: ${REDIS_HOST:localhost}port: ${REDIS_PORT:6379}orders:mail-from: ${ORDERS_MAIL_FROM:noreply@techlearningpro.example}@ConfigurationProperties(prefix = "orders")@Validatedpublic record OrdersProps(@NotBlank String mailFrom) {}# Run (local)# export DB_PASSWORD='***from-secret-store***'# export SPRING_PROFILES_ACTIVE=dev# java -jar orders-service.jar
Real-world use
An Orders hotfix once committed a staging password into application-staging.yml. Rotation was painful and git history retained the secret. Moving to ${DB_PASSWORD} plus a secret store mount made rotation a redeploy, not a scrubbing incident.
Security implications
Configuration is part of the attack surface.
- Leaked env dumps expose database and signing material.
- Restrict who can read Kubernetes Secrets / CI variable stores.
- Rotate after any accidental commit; treat history as compromised.
Best practices
- Required secrets have no defaults; optional hosts may default for local only.
- Validate
@ConfigurationPropertiesat startup with@Validated. - Prefer file mounts from a secrets manager when platforms support them.
Common mistakes
- Defaulting passwords in YAML so missing env silently uses a known value.
- Logging full
Environmentdumps that include secrets. - Mixing ConfigMap and Secret indiscriminately — non-secrets in Secrets obscure audits.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1BeginnerQuestionWhy prefer env vars over committed YAML for secrets?+
Answer
2IntermediateQuestionWhat is relaxed binding in Spring Boot?+
Answer
SPRING_DATASOURCE_URL map to nested properties such as spring.datasource.url with flexible case and separators.3IntermediateQuestionHow do you fail fast on missing config?+
Answer
@ConfigurationProperties so context refresh aborts before serving traffic.Hands-on exercise
Lab. Convert Orders datasource credentials to env placeholders. Add a validated @ConfigurationProperties record for orders.mail-from. Boot without DB_PASSWORD and capture the failure message.
Summary
Environment configuration keeps TechLearningPRO Orders 12-factor: placeholders, typed properties, and startup validation without committed credentials.