Last Updated : 23 Jul, 2025
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 NumberSometimes, 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
Example 2: Defining the Application Nameserver.port=8989
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 DatabaseTo connect to a MySQL database, you need to define the following properties:
Example 4: Connecting with an H2 Databasespring.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
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:
Example 5: Connecting with a MongoDB Databasespring.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
To configure a MongoDB connection, use the following properties:
Example 6: Connecting with an Eureka Serverspring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=BookStore
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:
Example 7: Connecting with a PostgreSQL Databaseeureka.client.register-with-eureka=true
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://localhost:9096/eureka/
eureka.instance.hostname=localhost
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.propertiesThe 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 DatabaseLet’s pick above example 3 where we were connecting with the MySQL Database, the corresponding properties will be as follows:
Case 2: Connecting with an Eureka Serverspring:
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
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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4