BBackend CourseLeroy Labs
0/0
Search the course

Configuration, profiles & environment variables

application.yaml, profiles, env-var placeholders, .env files, typed @ConfigurationProperties and failing fast on unsafe production config.

● beginnerSpring-Boot-StoreHostel-Web-AppReport-Manager-2IR3D (Kontentt)SperoTek (in dev)

The concept

The same build artifact (a jar or Docker image) must run in dev, staging and production. What changes between them is configuration: database URLs, secrets, feature flags and timeouts. The Twelve-Factor App rule is to keep config in the environment and out of the code.

Spring’s property sources, by precedence (highest wins)

  1. Command-line arguments (--server.port=9000)
  2. OS environment variables (SERVER_PORT=9000; relaxed binding maps SPRING_DATASOURCE_URL to spring.datasource.url)
  3. application-PROFILE.yaml
  4. application.yaml
  5. Defaults in code

Placeholders with defaults

spring:
  datasource:
    url: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/app}
jwt:
  expiration: ${JWT_EXPIRATION:1800}   # default when env var is absent

Profiles

SPRING_PROFILES_ACTIVE=prod loads application-prod.yaml on top of the base file and activates @Profile("prod") beans.

Typed configuration

@ConfigurationProperties(prefix = "app.jwt")
public record JwtProperties(Duration ttl, String issuer, Map<String, String> keys) {}

You get type conversion (for example "15m" becomes a Duration), validation, IDE autocompletion and one class to look at, instead of @Value strings scattered around.

In your projects

Hostel-Web-Appapplication.yaml · .env.example

Config is driven entirely by environment variables (SPRING_DATASOURCE_URL, JWT_SECRET, the VAPID_* keys, the CLOUDINARY_* keys), with the profile set by SPRING_PROFILES_ACTIVE (default dev). A local .env is loaded by springboot4-dotenv, and .env.example documents every variable. Production secrets come from AWS SSM through ECS (ecs.Secret.fromSsmParameter).

SperoTek (in dev)shared/startup/ProductionSecretsCheck.java · *Properties.java

SperoTek uses typed properties throughout (IdentityProperties, JwtProperties, RateLimitProperties, CryptoProperties, FrontendProperties). It also has a fail-fast guard: in the prod profile the app refuses to start if any secret is blank or still a dev default. That is far better than finding out in production that your JWT key is “changeme”.

IR3D (Kontentt)deploy/fetch-env.sh · scripts/push-env-to-ssm.sh

IR3D keeps production config in AWS SSM Parameter Store under a /kontentt/(env)/ prefix, and a script pulls it into the container’s environment at deploy time. Feature flags are plain properties: app.campaigns.auto-approve, app.media.signed-access.enabled.

Report-Manager-2AppEnvironment.java · StagingSafetyTest

Report-Manager-2 models the environment explicitly (production / staging / development). In staging, every outgoing email is redirected to a single safe inbox, and a test guarantees it. This stops staging from ever emailing real users.

Alternatives for storing config and secrets

Option Notes
.env and env vars (yours) Simple. Good for dev and small deployments
AWS SSM Parameter Store (IR3D, Hostel) Cheap, IAM-controlled, encrypted with KMS
AWS Secrets Manager Automatic rotation (Hostel’s RDS password uses fromGeneratedSecret)
HashiCorp Vault Dynamic short-lived secrets; heavier to run
Spring Cloud Config A central config server for many services
Kubernetes ConfigMaps and Secrets When running on k8s
  • Relaxed binding. app.jwt.ttl, APP_JWT_TTL and app.jwt-ttl all bind to the same property.
  • Feature flags. Turning behaviour on or off without a deploy (see the bonus module).
  • Immutable artifacts. Build once, configure per environment, and never rebuild just to change config.
  • Time zones. Report-Manager-2 sets APP_TIME_ZONE = "Africa/Accra" for business dates but stores timestamps in UTC. Module 3 explains why.

Check yourself

1. Which wins: the SERVER_PORT env var or server.port in application.yaml?
2. Why is SperoTek's ProductionSecretsCheck valuable?