Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Java builds (Maven, Gradle, JAR, WAR)

How do you configure a Spring Boot application for different environments?

BeginnerConceptJava builds (Maven, Gradle, JAR, WAR)
Answer

Short answer

Spring Boot reads settings from application.properties or application.yml. For different environments you use profiles (application-dev.yml, application-prod.yml) and override values with environment variables — which is how we pass settings in Docker and Kubernetes.

Explained in simple words

The same JAR should run in dev, staging and production. Only the settings change — database address, passwords, log level. So we never rebuild the app per environment; we give it different settings when it starts.

Example

# src/main/resources/application.yml — defaults
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/orders
    username: orders_user
    password: ${DB_PASSWORD}          # read from an environment variable
logging:
  level:
    root: INFO
# application-prod.yml — only what's different in production
logging:
  level:
    root: WARN

Ways to choose settings when starting the app:

# 1. Pick a profile
java -jar app.jar --spring.profiles.active=prod

# 2. Environment variables (Spring maps SPRING_DATASOURCE_URL → spring.datasource.url)
export SPRING_PROFILES_ACTIVE=prod
export SPRING_DATASOURCE_URL=jdbc:mysql://prod-db.internal:3306/orders
export DB_PASSWORD='s3cret'
java -jar app.jar

In Kubernetes:

env:
  - name: SPRING_PROFILES_ACTIVE
    value: prod
  - name: SPRING_DATASOURCE_URL
    value: jdbc:mysql://mysql.db.svc.cluster.local:3306/orders
  - name: DB_PASSWORD
    valueFrom:
      secretKeyRef: { name: orders-db, key: password }

Priority order (who wins)

Command-line arguments → environment variables → profile file (application-prod.yml) → default application.yml. So an environment variable overrides what's in the file.

What to say in the interview

"Spring Boot reads configuration from application.yml, with profile-specific files like application-prod.yml. We build one JAR and change behaviour per environment by setting SPRING_PROFILES_ACTIVE and overriding values with environment variables. In Kubernetes, normal settings come from ConfigMaps and passwords from Secrets, injected as environment variables — never hard-coded in the code or the image."

Follow-up questions you may get

  • How does Spring map env variables to properties? Upper-case and replace dots with underscores: spring.datasource.url → SPRING_DATASOURCE_URL.
  • Where do you check which config the app loaded? Startup logs show the active profile; the Actuator /actuator/env endpoint shows values (secure it in production).
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Java apps: Maven, Gradle, JAR & WAR

More on Java builds (Maven, Gradle, JAR, WAR)