Lesson 18 / 25

Profiles and External Configuration

One artifact, many environments.

application-{profile}.properties and precedence

Activate profiles (dev, test, prod) with spring.profiles.active to load application-prod.properties on top of the defaults. Configuration sources have a defined precedence: command-line arguments and environment variables override files packaged in the jar. Keep secrets out of the repository: inject them through environment variables or a secret store (Vault, cloud secret managers) at runtime. Build one artifact and configure it per environment.

Profile-specific settings

Not part of the built demo; check the Spring documentation for your version.

# application-prod.properties
spring.datasource.url=jdbc:postgresql://db.internal:5432/shop
spring.datasource.username=shop_app
# password comes from the environment: SPRING_DATASOURCE_PASSWORD
spring.jpa.hibernate.ddl-auto=validate
management.server.port=9090
logging.level.root=INFO

# run with:  java -jar shop.jar --spring.profiles.active=prod

Same jar everywhere

Rebuilding per environment hides differences; promote one tested artifact and change only configuration.

Quick check: Which overrides a value in application.properties inside the jar?

  • An environment variable or command-line argument
  • A comment in the code
  • The jar file name
  • Nothing can override it
Answer

An environment variable or command-line argument — External configuration wins.