Video in progress

Strong Junior 🌱 Spring

If a setting is defined in several places, which value does Spring Boot use?

There's an order: command-line arguments > env variables > profile file (application-prod.yml) > the common application.yml > @PropertySource > defaults. The one on the left overrides the one on the right.

📖 Answer

Simplified order (strongest to weakest):

  1. --server.port=9000 — command line;
  2. SPRING_APPLICATION_JSON;
  3. Java system property: -Dserver.port=9000;
  4. OS env variable: SERVER_PORT=9000;
  5. application-{profile}.yml outside the jar, then application.yml;
  6. application-{profile}.yml inside the jar, then application.yml;
  7. @PropertySource, SpringApplication.setDefaultProperties.

In practice it works like this: defaults live inside the jar, and on the server you override them with env variables — no rebuild needed.

Env names are matched through "relaxed binding": spring.datasource.url → SPRING_DATASOURCE_URL (dots become _, letters uppercase).

If you can't figure out where a value came from, /actuator/env (internal network only!) shows the source of every key.