patternjavaspringTip
application.yml vs application.properties: precedence and coexistence
Viewed 0 times
application.ymlapplication.propertiesconfig precedenceyaml vs propertiesspring configuration
Problem
Projects often have both application.properties and application.yml from different developers or migrations. Spring Boot loads both but properties files take precedence over YAML when both define the same key, causing hard-to-diagnose configuration surprises.
Solution
Choose one format per project and stick to it. When migrating, delete the old file after converting. If both must coexist temporarily, understand the load order:
YAML advantages: hierarchical structure, multi-document support, less repetition:
Properties equivalent (more verbose):
- application.properties (higher priority)
- application.yml (lower priority)
YAML advantages: hierarchical structure, multi-document support, less repetition:
spring:
datasource:
url: jdbc:postgresql://localhost/mydb
username: app
password: secretProperties equivalent (more verbose):
spring.datasource.url=jdbc:postgresql://localhost/mydb
spring.datasource.username=app
spring.datasource.password=secretWhy
Spring Boot's ConfigDataLoader loads files in a defined order and later sources override earlier ones. Within the same location, .properties files are listed after .yml in the default ordering, giving them higher priority.
Gotchas
- Profile-specific files (application-prod.yml) always override the base file regardless of format
- YAML does not support @PropertySource — use @PropertySource only with .properties files
- Multiline strings and lists are much more readable in YAML; complex structures in .properties get unwieldy
Revisions (0)
No revisions yet.