Configuration, profiles & environment variables
application.yaml, profiles, env-var placeholders, .env files, typed @ConfigurationProperties and failing fast on unsafe production config.
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)
- Command-line arguments (
--server.port=9000) - OS environment variables (
SERVER_PORT=9000; relaxed binding mapsSPRING_DATASOURCE_URLtospring.datasource.url) application-PROFILE.yamlapplication.yaml- 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
application.yaml · .env.exampleConfig 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).
shared/startup/ProductionSecretsCheck.java · *Properties.javaSperoTek 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”.
deploy/fetch-env.sh · scripts/push-env-to-ssm.shIR3D 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.
AppEnvironment.java · StagingSafetyTestReport-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 |
Related concepts you must know
- Relaxed binding.
app.jwt.ttl,APP_JWT_TTLandapp.jwt-ttlall 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.