Working with application.properties and application.yml files depending on our projects is part of our job as Spring Boot developers. Usage of both files are to write configurations for our project. Both have their own advantage, for enterprise level project application.yml is preferred.
Difference between application.properties and application.yml
| application.properties | application.yml |
|---|---|
| Supports flat and non-hierarchical structure | Supports hierarchical structure |
| Supports primitive types like strings and numbers | Supports Integer, Strings, Maps, and Lists |
| Key and values separated by equal | Contains key and value pairs |
| Limited programming language support | Wider programming language support |
| Preferred in smaller applications for its simplicity | Preferred for large and industry level applications |
Use application.properties and application.yml together in same project
- To use
application.propertiesandapplication.ymlin same project we need to add both files inresourcefolder of our project.
- Declare your configuration in both the files as show below.
server.port= 9000
app.name= .properties App
app.size.max= 50
app:
name: .yml APP
size:
max: 100
- When you execute your application it will load both files.
- First
application.ymlwill be loaded then it will loadapplication.properties. - If configuration items are present in both the files, configuration of
application.ymlwill be overwritten byapplication.properties. - For eg. In our case
app.nameis present in both files but when we run the applicationapp.namewill be “.properties app”.
System.out.println("Welcome to project: + " + appName + " | Max Size is: " + appSizeMax + "\n ");
Welcome to project: + .properties App | Max Size is: 50
Official Documentation : https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#appendix.application-properties
