Spring Boot - Application Properties
Spring Boot is built on top of the Spring Framework and includes all its features while simplifying configuration and setup. It has become a favorite among developers because it provides a rapid, production-ready environment that allows them to focus on business logic instead of dealing with complex configurations and setup.
Whenever you create a new Spring Boot application using Spring Initializr or an IDE (such as Eclipse or Spring Tool Suite), a file named application.properties is created inside the src/main/resources folder, which is shown below:

Geeks, now you must be wondering, what does this file do? What are its major roles during development? In a Spring Boot application, the application.properties file is used to define application-related properties. This file contains various configurations required to run the application in different environments, and each environment can have its own set of properties. Inside this file, we define different types of properties, such as changing the port, database connectivity, connection to the Eureka server, and many more. Now, let's see some examples for better understanding.
Example 1: Changing the Port Number
Sometimes, when you run your Spring Boot application, you may encounter an error like:

The error is Port 8989 was already in use. In this case, you can either stop the process running on this port or change the port number in your application.properties file, as shown below:

So as given in the above screenshot you can change your port number by the following line
server.port=8989
Example 2: Defining the Application Name
To define the name of your application, add the following property:
spring.application.name=userservice
This represents the property as a key-value pair, where each key is associated with a corresponding value.
Example 3: Connecting with a MySQL Database
To connect to a MySQL database, you need to define the following properties:
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
Example 4: Connecting with an H2 Database
H2 is an embedded, open-source, in-memory database. It is widely used for unit testing as it stores data in memory instead of persisting it on disk. To configure an H2 database, use the following properties:
spring.h2.console.enabled=true
spring.datasource.url=jdbc:h2:mem:dcbapp
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
Example 5: Connecting with a MongoDB Database
To configure a MongoDB connection, use the following properties:
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=BookStore
Example 6: Connecting with an Eureka Server
Eureka Server acts as a service registry for microservices. Every microservice registers with the Eureka server, which keeps track of all client applications running on different ports and IP addresses. The following properties configure the Eureka client:
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:9096/eureka/
eureka.instance.hostname=localhost
Example 7: Connecting with a PostgreSQL Database
PostgreSQL is a powerful, open-source relational database. To configure it in a Spring Boot application, use the following properties:
spring.datasource.url=jdbc:postgresql://localhost:5432/Postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
Note: The values provided are sample data. Please update them according to your database configuration. However, the keys remain the same.
Using application.yml Instead of application.properties
The application.properties file is not very readable when dealing with complex configurations. Most developers prefer using application.yml (YAML format) instead. YAML is a superset of JSON and provides a more structured and readable way to define hierarchical configuration data. Let's convert some of the previous examples into YAML format.
Case 1: Connecting with a MySQL Database
Let’s pick above example 3 where we were connecting with the MySQL Database, the corresponding properties will be as follows:
spring:
datasource:
url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
username: springuser
password: ThePassword
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
Case 2: Connecting with an Eureka Server
Let's pick above example 6 where we were connecting with the Eureka Server, the corresponding properties will be as follows:
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:9096/eureka/
instance:
hostname: localhost