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.
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.
Similar Reads
Difference Between JavaEE and Spring
JavaEE or J2EE also known as Java Enterprise Edition. J2EE Version 1.2 was developed as the first Enterprise specification in December 1999. In the year 2005 Sun renamed the Java Platform by dropping the name J2EE. Its core component is EJBs (Enterprise Java Beans) which is followed by JSP (Java Ser
3 min read
Difference Between URL and URN in Java
URL stands for Uniform Resource Location. A URL contains a protocol, server name or IP Address, Port Number, filename, or directory name. URL is a subset of URI as perceived from the image shown below that describes the network address or location where the source is available. URL begins with the n
3 min read
Difference between EJB and Spring
EJB and Spring both are used to develop enterprise applications. But there are few differences exists between them. So, in this article we have tried to cover all these differences. 1. Enterprise Java Beans (EJB) : EJB stand for Enterprise Java Beans. It is a server side software component that summ
3 min read
Difference Between YAML and JSON
YAML and JSON are data serialization formats. YAML is human-readable, uses indentation, supports comments, and is ideal for configuration files. JSON is compact, machine-readable, lacks comment support, and is commonly used in APIs and data exchange. YAMLYAML is a light-weight, human-readable data-r
2 min read
Difference between Spring MVC and Spring Boot
1. Spring MVC : Spring is widely used for creating scalable applications. For web applications Spring provides Spring MVC framework which is a widely used module of spring which is used to create scalable web applications. Spring MVC framework enables the separation of modules namely Model View, Con
3 min read
Difference between Spring and Spring Boot
Spring Spring is an open-source lightweight framework that allows Java developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier
4 min read
Difference between ServletConfig and ServletContext in Java Servlet
ServletConfig and ServletContext, both are objects created at the time of servlet initialization and used to provide some initial parameters or configuration information to the servlet. But, the difference lies in the fact that information shared by ServletConfig is for a specific servlet, while inf
3 min read
Reading and Writing Properties File in Java
The property file is a file we use in the Java Programming language to keep the configuration parameters. These files we called Resource Bundle because it is a bundle of resources that we are going to use in the application. What are the configuration parameters? Configuration parameters are the par
4 min read
Hibernate - Difference Between List and Bag Mapping
Hibernate supports both List and Bag Mapping and Set Mapping too. Hence there will be a tradeoff, regarding which is the best data type to use. The choice will be purely based on requirements but still, let us see the differences between them. Whenever there is a one-to-many relationship or many-to-
3 min read
Difference between Javabeans and Enterprise Javabeans
Notejava.beans: This package has been deprecated in Java 9 and later versions, in favor of using annotations and other modern ways of creating beans. Using it in production code is not recomended.JavaBeans: JavaBeans are recyclable software components for Java which by using a builder tool can be ma
2 min read