Open In App

Spring Boot – application.yml/application.yaml File

Last Updated : 22 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Spring is widely used for creating scalable applications. For web applications Spring provides. In Spring Boot, whenever we create a new Spring Boot Application in spring starter, or inside an IDE (Eclipse or STS) a file is located inside the src/main/resources folder named as application.properties file which is shown in the below media:

So in a spring boot application, application.properties file is used to write the application-related property into that file. This file contains the different configuration which is required to run the application in a different environment, and each environment will have a different property defined by it. Inside the application properties file, we define every type of property like changing the port, database connectivity, connection to the eureka server, and many more. But sometimes there is another file is located inside the src/main/resources folder named as application.yml/application.yaml file and the code present inside this file is present in a hierarchical format which is shown in the below image:

So what’s this application.yml file? YAML stands for Yet Another Markup Language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents. YAML is a data serialization language that is often used for writing configuration files. So YAML configuration file in Spring Boot provides a very convenient syntax for storing logging configurations in a hierarchical format. The application.properties file is not that readable. So most of the time developers choose application.yml file over application.properties file. YAML is a superset of JSON, and as such is a very convenient format for specifying hierarchical configuration data. YAML is more readable and it is good for the developers to read/write configuration files. 

Now let’s see some examples for better understanding via proposing different examples as listed and described later as follows:

  1. To Change the Port Number
  2. To define the name of our application
  3. Connecting with the MySQL Database
  4. Connecting with the H2 Database
  5. Connecting with the MongoDB Database
  6. Connecting with the Eureka Server

Example 1: To Change the Port Number

Sometimes when you run your spring application you may encounter the following type of error

The error is Port 8989 was already in use. So in this case you may kill that process that is running on this port number or you may change your port number and rerun your application. So where do you have to change your port number? e.g in the application.properties file or in application.yml file. So you can change your port number by the following line

server:
  port:
    8082

Example 2: To define the name of our application

To define the name of our application you can write the properties like this

spring:
  application:
    name: userservice

So you can see this represents the property as key-value pair here, every key associated with a value also.

Example 3: Connecting with the MySQL Database

To connect with the MySQL Database you have to write a bunch of lines. You can write the properties like this

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    username: springuser
    url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
    password: ThePassword
  jpa:
    hibernate:
      ddl-auto: update

Example 4: Connecting with the H2 Database

H2 is an embedded, open-source, and in-memory database. It is a relational database management system written in Java. It is a client/server application. It is generally used in unit testing. It stores data in memory, not persist the data on disk. To connect with the H2 Database you have to write a bunch of lines. You can write the properties like this

spring:
  h2:
    console:
      enabled: 'true'
  datasource:
    username: sa
    url: jdbc:h2:mem:dcbapp
    driverClassName: org.h2.Driver
    password: password
  jpa:
    database-platform: org.hibernate.dialect.H2Dialect

Example 5: Connecting with the MongoDB Database

To connect with the MongoDB Database you have to write a bunch of lines. You can write the properties like this

spring:
  data:
    mongodb:
      database: BookStore
      port: '27017'
      host: localhost

Example 6: Connecting with the Eureka Server

Eureka Server is an application that holds information about all client-service applications. Every Microservice will register into the Eureka server and the Eureka server knows all the client applications running on each port and IP address. Eureka Server is also known as Discovery Server. You can write the properties like this

eureka:
  client:
    service-url:
      defaultZone: http://localhost:9096/eureka/
    fetch-registry: 'true'
    register-with-eureka: 'true'
  instance:
    hostname: localhost

Note: The value written here is sample data. Please write the values as per your requirements. But the keys remain the same. 

Tip:  If you want to convert your application.properties file code to application.yml file then you can google and select some online tool for doing this.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads