application.yml vs application.properties in Spring Boot
Introduction
Spring Boot externalizes configuration. You can use application.properties (flat key=value) or application.yml (hierarchical, readable). Both work. This series uses YAML for nested blocks (datasource, JPA, logging) without repeating long prefixes.
Real-world explanation
- Properties — One line per key.
spring.datasource.url=jdbc:...is fine for a few lines; it gets long when you add 30 keys. - YAML — Indentation defines tree structure. Easy to read and comment. Watch indentation (use spaces, not tabs, per YAML spec).
- Order of loading —
application.propertiesandapplication.ymlinsrc/main/resourcesboth load. Profile-specific filesapplication-dev.ymloverride base keys whendevis active. Same rules apply to.properties.
Step-by-step: one base file
src/main/resources/application.yml (skeleton, filled more in next articles)
spring:
application:
name: demo-service
jpa:
hibernate:
ddl-auto: validate
show-sql: false
open-in-view: false
datasource:
url: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/demo}
username: ${SPRING_DATASOURCE_USERNAME:demo}
password: ${SPRING_DATASOURCE_PASSWORD:demo}
server:
port: 8080Same in properties (for comparison)
spring.application.name=demo-service
spring.jpa.hibernate.ddl-auto=validate
server.port=8080Why we prefer YAML here
- Datasource and JPA sit under
spring, easier to scan in code review - You can add
spring.profiles: in YAML you often do profile-specificapplication-prod.yml(article 21)
Common mistakes
- Tab characters in YAML: parsers fail with obscure errors. Use 2 spaces as team standard.
- Two keys for the same thing in two files; last loaded wins, which is confusing in debugging.
- Putting secrets in the repo. Use environment variables:
${VAR:default}as above.
Best practices
ddl-auto: validateornonein production;updateis only a dev convenience, know the trade-off.- Disable
spring.jpa.open-in-viewin APIs—prevents lazy loading surprises in controllers.
Final summary
Use .yml or .properties; pick one style per project. This series standardizes on application.yml, profile files like application-dev.yml, and placeholders for secrets. Next: configuration discipline beyond syntax.