In Spring Boot applications, configuration files are used to define application-level settings such as server port, database credentials, logging levels, and environment-specific properties. Spring Boot supports two primary configuration formats:
application.yml
application.yml uses YAML (Yet Another Markup Language), which supports hierarchical and structured configuration. It is more readable when dealing with complex configurations.
Example:
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/testdb
username: root
password: admin
2. .properties File
application.properties uses a simple key–value pair format to configure Spring Boot applications. It is easy to understand and commonly used for small or simple configurations.
Example:
server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=admin
Which One Should You Use?
Use .yml When:
- You need hierarchical configurations.
- Your application has complex nested structures.
- You want cleaner and shorter config files.
- Config files are shared across multiple languages or tools.
Use .properties When:
- You prefer simplicity.
- Your configuration is flat and minimal.
- You rely on @PropertySource.
- You want maximum compatibility with legacy Java tools.
application.yml vs application.properties
| Feature | YAML(.yml) | .properties |
|---|---|---|
Specification | Well-defined YAML specification | No formal spec (based on Java documentation) |
Readability | Highly readable | Readable |
Data Types | Supports scalar, list, and map types | Values treated as strings |
Structure | Hierarchical | Flat (dot-based) |
List Support | Native | Convention-based |
Language Usage | Used across many languages | Primarily Java |
@PropertySource Support | Not supported | Supported |
Spring Profiles | Multiple profiles in one file | One file per profile |
Type Safety | Preserves actual data types | Values resolved as strings |
Verbosity | Less verbose | More verbose |