Difference between application.properties vs Application.json vs Application.yml in Spring Boot

Hello guys, if you have worked in any Spring Boot application then you may have noticed that there are multiple ways to declare configuration for Spring Boot application. I am sure you must have noticed application.propties, application.json, and application.yml while working in proprietary or open source Spring Boot application. When I first started, it was application.propties and it was quite familiar because I have used properties file in past for specify configuration of Java application but when I first see applcation.json and application.yml I was bit confused like what they are and does they really work? In the end I found out that they are just different ways to specify configuration for any Spring Boot application. 

Since many Java developer already asked me this question, I thought to write and share the difference between them and in this article, we are going to take a look at the difference between widely appreciated configuration files use for Spring applications these include, application.properties, application.json, and application.yml. 

We start our understanding by defining what is a configuration file? What is the need to have different configuration files? What advantages does one offer over another?

In this article we will see various configuration files along with their syntax and how to use them in your project. Before closing the chapter, we will demonstrate it with the help of a simple REST API using Spring Boot to access the properties specified in the configuration file. Hope this entire journey will help you to pick the right configuration file for your project.
 

What is YAML?

YAML stand for Yet Another Markup Language. YAML is a human-readable language. Its syntax is quite simple as if, it is written in plain English language. It follows a key-value pattern for defining data. 

It is frequently utilized for configuration files and in programs where data is being delivered or saved. Numerous parts of YAML are derived from well-known computer languages like XML, HTML, C, and others. When it comes to indentation, it makes you think of Python. 

We cannot use braces, brackets, or any other closing tags in it, just as in Python. We define YAML documents using the.yml or .yaml extension.

YAML syntax
Here is how a YAML file look like and they are saved using .yml extension like application.yml. YAML is also extensively used in Ansible playbook definition. 
name: John
school: X high school
age: 18
registered_since: 2008
teams:
- Cricket
- Football
- Hockey
interest:
- Football


Why use YAML for Configuration in Spring Boot?

Although YAML may be used with several computer languages, creating configuration files is the most frequent and well-liked use. But you might be wondering why you left JSON in the first place. 

It is recommended that configuration files be written in YAML rather than JSON since it is simpler to comprehend and more user-friendly, however, they can often be used interchangeably. 

Another legitimate benefit of YAML is that it makes it possible for source control solutions like GitHub to keep track of and carry out audits of updates.

Application.properties vs Application.json vs Application.yml in Spring Boot




Application.properties vs Application.json vs Application.yml in Spring Boot

Now, let's checkout what does application.properties and application.yml do and what are differences between them in Spring Boot:
  

1. What is Application Properties?

As we all know Spring Boot is built on top of spring. The framework reduces the need for writing boilerplate code, rather it enables developers to focus on the business logic instead of managing dependencies and configurations. 

Every spring boot application comes with a file named application.properties. You might be wondering what is the primary function of this file. The answer to this is it holds application-related properties. This file provides many configurations needed to execute the program in various environments, each of which will specify a separate set of properties. 

We can specify various types of properties, such as altering the port, database connectivity, hibernate properties, connection to the Eureka server, etc.

Application.properties syntax

student.name = some-name
student.age = some-age
student.school = name-of-school
student.teams = hockey, cricket, football
student.favourite-subject = subject-name
student.hobbies = student-hobby-1, student-hobby-2, student-hobby-3, and so on


How to write configuration in properties file?

Now, let's see few more example of declaring configuration in application.properties file in Spring Boot?  

#Port Number
spring.port=8081

#Application name
spring.application.name=your_app_name

#Hibernate
spring.jpa.hibernate.ddl-auto=update

# Connectivity with MySQL database
spring.datasource.url = jdbc:mysql://${MYSQL_HOST:localhost}:3306


Difference between application.properties vs application.yml in Spring Boot

Both files are used for defining configuration properties that are consumed by the spring application. But it is widely believed that the application.properties file is hard to read and understand. Therefore, developers prefer application.yml files over application.properties files most of the time. 

Given that YAML is a superset of JSON, it is a particularly useful format for describing hierarchical configuration data. Take a few configurations to form an application.properties file and re-write them in the YAML file.

#Connectivity with MySQL database
spring:
datasource:
url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/your_database_name
username: database_username
password: database_password
driver-class-name: com.mysql.jdbc.Driver
jpa:
hibernate:
ddl-auto: update



What is Application.json file in Spring Boot?

Spring Boot enables you to externalize your configuration which means you can work in different environments. You can use various sources for external configuration such as Java properties files, YAML files, environment variables, application json, and command-line arguments.

As mentioned earlier Spring Boot allows a developer to declare an application’s configuration in a variety of formats. Properties from the application.json file are one such way to load configurations as opposed to application.properties or application.yml. 

Even though reading from json configuration is possible in spring boot. But it is important to note that spring boot doesn’t support this method by default. Therefore one has to put extra effort to make it work.


How to write configuration in application json in Spring Boot?

Many times, limitations on environment variables and system properties prevent the usage of certain property names. Spring Boot offers you the option to combine a group of properties into a single JSON structure to aid with this. 

Every time your spring application start attributes from spring.application.json will be processed and added to the Environment. One can specify spring.application.json properties using the command line system as an environment variable, this can be seen below.

Using command line argument

$ java -jar myapp.jar --spring.application.json='{
"spring":
{
"datasource": {
"url": "jdbc:mysql://${MYSQL_HOST:localhost}:3306/your_database_name",
"username": "database_username",
"password": "database_password",
"driver-class-name": "com.mysql.jdbc.Driver"
}
}
}'




Application.json syntax

--Writing custom properties
{
"student": {
"name": "some-name",
"age": "some-age",
"school": "name-of-school",
"favourite-subject": "subject-name",
"hobbies": [
"student-hobby-1", "student-hobby-2", "student-hobby-3", "student-hobby-4"
]
}
}

--MySQL database connectivity
{
"spring":
{
"datasource": {
"url": "jdbc:mysql://${MYSQL_HOST:localhost}:3306/your_database_name",
"username": "database_username",
"password": "database_password",
"driver-class-name": "com.mysql.jdbc.Driver"
}
}
}


That's all about difference between application.properties, application.yml, and application.json in Spring Boot application. With this, our article is at its conclusion. This brief essay aims to explain the differences between the three configuration files. 

Here we learn what these files are and why they are used so frequently to write the spring boot application’s configuration. Moving ahead we conducted a brief comparison regarding the syntax of properties and YAML files. This article also shed light on how to externalize Spring Boot configuration with the help of application.json file.

I hope this post has assisted you in determining how the three configuration files differ from one another and which one suits your project well.


Other Java and Spring Articles, Resources and Tutorials you may like to explore

Thanks for reading this article so far. If you like my explanation of difference between different ways to create Spring Boot configuration file like application.properties, appication.json, and application.yml  then please share them with your friends and colleagues. If you have any questions or feedback, then please drop a note.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.