Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 30 Jun, 2021

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. 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

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 hereIt 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 JavaIt is primarily used in java
Hierarchical StructureNon-Hierarchical Structure
Spring Framework doesn’t support @PropertySources with .yml filessupports @PropertySources with .properties file
If you are using spring profiles, you can have multiple profiles in one single .yml fileEach 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 configurationWhile 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.
 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!