Spring Boot lets you configure your app without touching code by using configuration files. These files live in the src/main/resources
folder and help control:
- Server port
- Database settings
- Logging levels
- Custom application properties
The two most commonly used Spring Boot Properties formats are:
application.properties
application.yml
Letβs explore how they differ and how to use them effectively.

π What is application.properties
?
A simple key-value file format used for defining configuration.
π§ Example:
1 2 3 4 5 6 7 |
server.port=8081 spring.datasource.url=jdbc:mysql://localhost:3306/testdb spring.datasource.username=root spring.datasource.password=secret logging.level.org.springframework=DEBUG |
β Pros:
- Simple to read/write for small apps
- Easy to grep or parse
- Familiar for traditional Java devs
π What is application.yml
?
YAML (YAML Ainβt Markup Language) is a structured, hierarchical configuration format.
π§ Same Example in YAML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
server: port: 8081 spring: datasource: url: jdbc:mysql://localhost:3306/testdb username: root password: secret logging: level: org: springframework: DEBUG |
β Pros:
- Better for complex/nested properties
- More readable for large configs
- Preferred in Kubernetes and cloud-native apps
π Key Differences
Feature | application.properties | application.yml |
---|---|---|
Format | Key-Value | Hierarchical (YAML) |
Readability | Good for small configs | Better for structured configs |
Multi-profile support | Slightly verbose | Cleaner with nesting |
Error sensitivity | Fewer formatting errors | Sensitive to indentation |
Community trend | Older Spring projects | Increasingly popular |
π― When to Use Which?
- Use
application.properties
if:- You prefer key-value style
- Your configs are flat and simple
- Youβre maintaining legacy code
- Use
application.yml
if:- You need nested properties
- You use multiple profiles
- You want cleaner config for larger apps
β Both formats are fully supported by Spring Boot β itβs a matter of preference.
π₯ Multi-Profile Support
You can create profile-specific files like:
application-dev.properties
application-prod.yml
Activate a profile via:
1 2 3 |
spring.profiles.active=dev |
Or use --spring.profiles.active=prod
when starting your app.
π Accessing Custom Properties
Example:
1 2 3 |
app: welcome: Hello from YAML! |
You can bind this to a class:
1 2 3 4 5 6 7 8 9 |
@Component @ConfigurationProperties(prefix = "app") public class AppProperties { private String welcome; // Getter and Setter } |
Make sure you add:
1 2 |
@EnableConfigurationProperties(AppProperties.class) |
π« Common Mistakes to Avoid
Mistake | Problem |
---|---|
Indentation error in YAML | Causes app to fail silently or misread properties |
Mixing application.yml and .properties unintentionally | May lead to confusion if overlapping values |
Not setting active profile | Spring uses default profile, might load wrong config |
Hardcoding sensitive values | Use environment variables or vaults for secrets |
β Summary
Topic | Key Takeaway |
---|---|
Properties vs YAML | Choose based on readability and structure |
Profiles | Use application-{profile}.yml/properties |
Accessing custom values | Use @ConfigurationProperties |
Security | Avoid storing secrets directly |
Whether you choose .properties
or .yml
, Spring Boot Properties gives you the power to tweak your app without touching the code.