Video in progress

If the same property is set in application.yml, in an env var and in a command-line argument, which one does Spring Boot take?

The command-line argument wins, then the env var, and last the application.yml inside the jar. The general rule: a source supplied from outside and "closer" to the running process overrides the one inside.

📖 Answer

# in application.yml: server.port: 8080
export SERVER_PORT=9090
java -jar app.jar --server.port=7070
# Tomcat started on port 7070

A simplified order (the higher one wins):

  1. --server.port=... command-line arguments;
  2. SPRING_APPLICATION_JSON;
  3. Java system properties (-Dserver.port=...);
  4. OS environment variables;
  5. application-{profile}.yml outside the jar;
  6. application.yml outside the jar;
  7. application-{profile}.yml inside the jar;
  8. application.yml inside the jar.

Note: since Boot 2.4, a plain application.yml outside the jar beats even the profile file inside the jar. Older articles list the order differently.

This order is why one image works in every environment: the defaults are inside the jar, and the environment-specific values are in the Kubernetes env.

A situation that confuses people when debugging: "I changed the yml and nothing happened." The cause is often a forgotten env var in the deployment manifest or in a ConfigMap. /actuator/env (only if it is exposed on the internal network alone) shows exactly which source a value came from.