application.properties vs application.yml vs Environment Variables
Fast recall
Quick revision
Core answer
Environment with a strict precedence order: command-line args > environment variables > profile-specific files > base `application.{yml,properties}` > defaults. application.yml supports hierarchical structure and multi-document files; application.properties is flat key=value pairs — both have the same precedence, so the file loaded last wins for duplicates. Environment variables use relaxed binding (SPRING_DATASOURCE_URL maps to spring.datasource.url) and are essential for twelve-factor app deployments where credentials and per-environment values must be injected at runtime, never committed to source.Whether the candidate knows precedence rules and can decide which mechanism to use in a given environment (local dev vs CI vs production Kubernetes)
Assuming `application.yml` always wins over `application.properties` — in fact they have the same precedence, and if both exist Spring Boot loads both with properties processed first, so yml values win for duplicates
Mention profile-specific files (`application-prod.yml`), Spring Cloud Config Server, and the full PropertySource order documented in Spring Boot reference docs
Interview-ready guide
Interview answer
Spring Boot aggregates configuration from multiple sources with a strict priority order. Knowing the order is essential for understanding why a property has a particular value in a given environment.
Priority order (highest to lowest)
- Command-line arguments (
--server.port=9090) — always win - Environment variables (
SERVER_PORT=9090) — override property files; the standard for Docker and Kubernetes - Profile-specific files (
application-prod.properties) — override the base file for active profiles application.properties/application.yml— the base defaults
Properties vs YAML
application.properties: flat key-value, readable, no indentation sensitivityapplication.yml: hierarchical structure, good for deeply nested config; but YAML null vs empty string behavior is a common trap —key:is null, which causes@Value("${key}")to throwIllegalArgumentException- If both files exist at the same location, properties is loaded first; yml takes precedence for any key in both
Environment variable relaxed binding
SPRING_DATASOURCE_URLautomatically maps tospring.datasource.url— dots and dashes become underscores; uppercase is standard for container environments
Verifying configuration
/actuator/envshows every property source in priority order so you can see exactly which value was resolved--debugprints the conditions evaluation report for auto-configuration
Use property files for developer defaults, profile-specific files for environment overrides, and environment variables or secrets managers for production credentials — never hardcode credentials in any property file committed to version control.
application.properties vs application.yml vs Environment Variables
Direct Answer
Spring Boot loads configuration from many sources and merges them into a unified Environment with a strict precedence order: command-line args > environment variables > profile-specific files > base application.{yml,properties} > defaults. application.yml supports hierarchical structure and multi-document files; application.properties is flat key=value pairs — both have the same precedence, so the file loaded last wins for duplicates. Environment variables use relaxed binding (SPRING_DATASOURCE_URL maps to spring.datasource.url) and are essential for twelve-factor app deployments where credentials and per-environment values must be injected at runtime, never committed to source.
Common Mistakes & Pitfalls
Assuming application.yml always wins over application.properties — in fact they have the same precedence, and if both exist Spring Boot loads both with properties processed first, so yml values win for duplicates
Check your understanding
Answer these without looking back at the article.
- How does Spring Boot resolve a property named `server.port` when it is set in both `application.properties` and as an environment variable `SERVER_PORT`?
- What is relaxed binding and how does it help with environment variable naming?
- How do profile-specific configuration files (`application-prod.yml`) interact with the base `application.yml`?
- How do you verify which property source resolved a value at runtime?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.