Spring Boot - application.yml/application.yaml File
Last Updated :
22 Dec, 2021
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:
- To Change the Port Number
- To define the name of our application
- Connecting with the MySQL Database
- Connecting with the H2 Database
- Connecting with the MongoDB Database
- 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.
Similar Reads
Spring Boot - Caching Spring Boot is a project that is built on top of the Spring Framework that provides an easier and faster way to set up, configure, and run both simple and web-based applications. It is one of the popular frameworks among developers these days because of its rapid production-ready environment which e
6 min read
Spring Boot - Starter Web Today most of the applications demand Model-View-Controller (MVC) architecture to meet the various needs like handling user data, making application efficient, to provide dynamic nature to applications. It was basically used for building desktop graphical user interfaces (GUIs), but now is becoming
6 min read
Spring Boot - Difference Between @Service Annotation and @Repository Annotation Spring Annotations are a form of metadata that provides data about a program. Annotations are used to provide supplemental information about a program. It does not have a direct effect on the operation of the code they annotate. It does not change the action of the compiled program. @Service Annota
7 min read
Unit Testing in Spring Boot Project using Mockito and Junit Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
6 min read
Spring Boot - Thymeleaf with Example Thymeleaf is a server-side Java-based template engine for both web and standalone environments, capable of processing HTML, XML, JavaScript, CSS and even plain text. It is more powerful than JPS and responsible for dynamic content rendering on UI. The engine allows a parallel work of the backend and
6 min read
Spring Boot - MongoRepository with Example Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
5 min read
Spring Boot - Integrating Hibernate and JPA Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
3 min read
Spring Boot JpaRepository with Example Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and se
9 min read
Spring Boot - CRUD Operations using MongoDB CRUD stands for Create, Read/Retrieve, Update, and Delete and these are the four basic operations that we perform on persistence storage. CRUD is data-oriented and the standardized use of HTTP methods. HTTP has a few methods which work as CRUD operations and do note they are very vital from a develo
5 min read
Spring Boot - Spring Data JPA Spring Data JPA or JPA stands for Java Persistence API, so before looking into that, we must know about ORM (Object Relation Mapping). So Object relation mapping is simply the process of persisting any java object directly into a database table. Usually, the name of the object being persisted become
6 min read