How to use application.properties and application.yml together

How To Use application.properties and application.yml together

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.propertiesapplication.yml
Supports flat and non-hierarchical structureSupports hierarchical structure
Supports primitive types like strings and numbersSupports Integer, Strings, Maps, and Lists
Key and values separated by equalContains key and value pairs
Limited programming language supportWider programming language support
Preferred in smaller applications for its simplicityPreferred for large and industry level applications
application.properties VS application.yml

Use application.properties and application.yml together in same project

  • To use application.properties and application.yml in same project we need to add both files in resource folder of our project.
    Use application.properties and application.yml together in same 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.yml will be loaded then it will load application.properties.
  • If configuration items are present in both the files, configuration of application.yml will be overwritten by application.properties.
  • For eg. In our case app.name is present in both files but when we run the application app.name will 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