How do you pass different configuration to a Spring Boot JAR in dev, staging and production without rebuilding it?
Short answer
Build one JAR and change its configuration from the outside: Spring profiles (application-prod.yml selected by SPRING_PROFILES_ACTIVE=prod), environment variables (Spring maps SPRING_DATASOURCE_URL to spring.datasource.url), command-line arguments, or mounted config files from a Kubernetes ConfigMap.
Spring Boot's priority order (highest wins, simplified)
- Command-line args:
--server.port=9090 - Environment variables:
SERVER_PORT=9090 application-{profile}.ymloutside / inside the JARapplication.ymlinside the JAR
Example
# application.yml (inside the JAR — safe defaults)
server.port: 8080
spring.datasource.url: jdbc:postgresql://localhost:5432/orders
# application-prod.yml
logging.level.root: WARN
# Kubernetes Deployment
env:
- name: SPRING_PROFILES_ACTIVE
value: prod
- name: SPRING_DATASOURCE_URL
value: jdbc:postgresql://orders-db:5432/orders
- name: SPRING_DATASOURCE_PASSWORD
valueFrom: { secretKeyRef: { name: orders-db, key: password } }
Why not rebuild per environment?
If staging and prod run different binaries, you never really tested what you deploy. "Build once, deploy many" means the exact artifact that passed staging goes to production.
What to say in the interview
"We build the JAR once and externalise config. The profile is chosen with SPRING_PROFILES_ACTIVE, non-secret settings come from env vars or a ConfigMap, and passwords from a Kubernetes Secret. Spring's relaxed binding maps env vars like SPRING_DATASOURCE_URL to properties automatically."