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


Infrequently a SpringBoot developer may need an external configuration to define features for SpringBoot applications so that we can use the same application code in different environments. For this purpose, we can use YAML and .properties files that are used to store the required features. Despite similar functionality, there are a few distinctions between them in terms of syntax and additional features. In this article, we are going to explore what are the main differences that exist between .yml and .properties files.

YAML vs Properties Files

In this section, we will introduce the YAML and properties files and later, we will create a table describing the differences between these two files.

YAML

It is an acronym that stands for YAML Ain't Markup Language. In some books and articles, its full form is written as Yet Another Markup Language, which is also correct. The YAML is a human-readable data serialization format that uses indentation to indicate structure and colons to separate key-value pairs.

Spring profiles, a core component of the Spring framework, allow us to segregate parts of our application to make it available in particular environments only. For this purpose, we need only a single YAML file because it can include multiple profiles in the same file.

Sample YAML

spring:
   application:
      name: spring1
   datasource:
      url: jdbc:mysql://localhost:9092/databasename
      username: myname
      password: myPasswrd

The above box contains the content of a sample YAML file. In the 'application' section, we have set the name of our application and in the 'datasource' section, we have set the username, password and URL for the database.

Properties

They are simple text files that uses the format key=value to store configuration properties. Each property is on a new line and comments start with a hash (#). Like YAML, it also provides the configuration required to execute the application.

By default, spring boot supports .properties files. Suppose, we create both .yml and .properties files for a single application, then spring boot will give priority to .properties files over .yml and executes it rather than .yml.

Sample Properties

# name of the application
spring.application.name= spring1
# making connection with database
spring.datasource.url= jdbc:mysql://localhost:9092/databasename
spring.datasource.username= myname
spring.datasource.password= myPasswrd

This is the .properties representation of YAML format. We can clearly see that its format is completely different from YAML and also it is difficult to understand. The text with # symbol specifies comments.

Difference between YAML and Properties Files

From the above discussion, we can conclude the following differences between YAML and properties files:

YAML

Properties

The extension for this type of file is .yml.

The extension for this type of file is .properties.

It needs proper indentation otherwise we will encounter errors.

Indentation is not required in this file.

In YAML files, it is not necessary to repeat the same keys again and again.

In properties files, the keys get repeated in each line.

It is more human readable than a properties file.

It is a comparatively less human readable format.

YAML files support the hierarchical format.

properties files support the sequential format.

It can support multiple languages including Java, Python and Ruby.

It can support only a single programming language, i.e. Java.

We can include multiple spring profiles in a single YAML file.

For every environment of the spring profile, we need separate properties files.

Conclusion

In this article, we have learned the difference between YAML and properties files. Both files are used for defining the features of SpringBoot applications. Using YAML files have more advantages than properties files that's why developers prefer YAML files over properties files.

Updated on: 17-Aug-2023

749 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements