Video in progress

How do you separate dev and prod settings using Spring profiles?

I create application-dev.yml and application-prod.yml for each environment and turn on the right profile through SPRING_PROFILES_ACTIVE. Shared settings stay in the main application.yml.

📖 Answer

src/main/resources/
  application.yml         # shared by all
  application-dev.yml     # local H2, debug logs
  application-prod.yml    # Postgres, fewer logs
# application-prod.yml
spring:
  datasource:
    url: jdbc:postgresql://db:5432/shop
logging:
  level:
    root: WARN

Running it:

java -jar app.jar --spring.profiles.active=prod
# or in a container
SPRING_PROFILES_ACTIVE=prod

A value in the profile file overrides the one in the main file. You can also tie things to a profile in code: a bean marked with @Profile("dev") is created only in dev.

Be careful: writing the prod password into application-prod.yml and committing it to git is the most common mistake. Keep only non-secret values in the profile file, and let passwords come from outside.