Open In App

Spring Boot – Application Properties

Last Updated : 22 Sep, 2023
Like Article

As we already know 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 it’s a rapid production-ready environment that enables the developers to directly focus on the logic instead of struggling with the configuration and set-up. In Spring Boot, whenever you 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 file which is shown in the below image as shown below as follows:

Geeks, now you must be wondering what does this file do? What are the major roles of this file during development? So in a spring boot application, 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. Now let’s see some examples for better understanding.

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 file.

So as given in the above screenshot you can change your port number by the following line 


Example 2: To define the name of our application

To define the name of our application you can write the properties like this = 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

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


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

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


Example 7: Connecting with the PostgreSQL

PostgreSQL is a free and open-source relational database management system. To connect with the PostgreSQL Database you have to write a bunch of lines. You can write the properties like this.

spring.datasource.password=postgres = org.hibernate.dialect.PostgreSQLDialect

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

application.yml/application.yaml file

The file is not that readable. So most of the time developers choose application.yml file over 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. For example, let’s pick some of the properties files that we have explained above, and let’s write them in YAML format.

Case 1: Let’s pick above example 3 where we were connecting with the MySQL Database, the corresponding properties will be as follows:

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

Case 2: Let’s pick above example 6 where we were connecting with the Eureka Server, the corresponding properties will be as follows:

register-with-eureka: true
fetch-registry: true
defaultZone: http://localhost:9096/eureka/
hostname: localhost

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads