Spring Boot Tutorial 0/110 lessons ~6 min read Lesson 51

    Environment Configuration

    Twelve-factor Orders services configure via the environment, not committed secrets.

    Course progress0%
    Focus
    12 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    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_URL maps to db.url / nested properties.
    • priority: command line > env > profile YAML > base YAML.
    • typed config: @ConfigurationProperties with validation annotations.
    • secrets: mount or inject; never commit real values.

    Step-by-step explanation

    1. Replace hard-coded JDBC and mail credentials with ${...} placeholders.
    2. Create OrdersDataSourceProperties (or use Spring Boot's datasource props) with validation.
    3. Document required env vars in the service README for Ops.
    4. Start without DB_PASSWORD and confirm Boot fails clearly.

    Informative example

    Env-backed Orders datasource and typed Stripe-free payment placeholder config.

    bash
    # application.yml
    spring:
    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")
    @Validated
    public 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 @ConfigurationProperties at 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 Environment dumps 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.

    3 questions
    1BeginnerQuestionWhy prefer env vars over committed YAML for secrets?+

    Answer

    Git is replicated and retained; env/secret stores support rotation, least privilege, and audit without rewriting history.
    2IntermediateQuestionWhat is relaxed binding in Spring Boot?+

    Answer

    Env names like 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

    Omit defaults for required placeholders and validate @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.

    Ready to mark this lesson complete?Track your journey across the entire course.