Open In App

Difference between YAML(.yml) and .properties file in Java SpringBoot

Improve
Improve
Like Article
Like
Save
Share
Report

These are the files to have different configurations properties required to make the application up and run like to connect with the database what are the credentials, on which port the application will run, etc. 

YAML (.yml) File: YAML is a configuration language. Languages like Python, Ruby, Java heavily use it for configuring the various properties while developing the applications. 

If you have ever used Elastic Search instance and MongoDB database, both of these applications use YAML(.yml) as their default configuration format. 

Example:  

#.yml file

some_key:value
some_number:9
some_bool:true

Nesting: For nesting, the .yml file support hierarchy using spaces.  

# .yml file

somemap:
  key:value #use space not tab
  number:9

#inline format                         
  map2: {bool=true, date=2016-01-01}

Let’s define a list in such files: YAML as part of its specification supports the list.  

#.properties file

# A List
numbers[0] = one
numbers[1] = two

# Inline List
numbers = one, two

.properties File: This file extension is used for the configuration application. These are used as the Property Resource Bundles files in technologies like Java, etc. 

Example:  

#.properties file

some_key = value
some_number = 9
some_bool = true

Nesting: For nesting, the .properties file support dot(.) notation. The inline format in the .yml file is very similar to JSON 

#.properties file

somemap.key = value 
somemap.number = 9

map2.bool = true
map2.date = 2016-01-01

Let’s define a list in such files: .properties file doesn’t support list but spring uses an array as a convention to define the list in the .properties file.  

#.yml file

numbers:
  - one # use a dash followed by space
  - two

# Inline List
numbers:[one, two]

Table of Difference:  

YAML(.yml) .properties
Spec can be found here It doesn’t really actually have a spec. The closest thing it has to a spec is actually the javadoc.
Human Readable (both do quite well in human readability) Human Readable
Supports key/val, basically map, List and scalar types (int, string etc.) Supports key/val, but doesn’t support values beyond the string
Its usage is quite prevalent in many languages like Python, Ruby, and Java It is primarily used in java
Hierarchical Structure Non-Hierarchical Structure
Spring Framework doesn’t support @PropertySources with .yml files supports @PropertySources with .properties file
If you are using spring profiles, you can have multiple profiles in one single .yml file Each profile need one separate .properties file
While retrieving the values from .yml file we get the value as whatever the respective type (int, string etc.) is in the configuration While in case of the .properties files we get strings regardless of what the actual value type is in the configuration

What should I use .properties or .yml file? 

Strictly speaking, .yml file is advantageous over .properties file as it has type safety, hierarchy and supports list but if you are using spring, spring has a number of conventions as well as type conversions that allow you to get effectively all of these same features that YAML provides for you. 

One advantage that you may see out of using the YAML(.yml) file is if you are using more than one application that read the same configuration file. you may see better support in other languages for YAML(.yml) as opposed to .properties.
 


Last Updated : 30 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads