What is properties file in Spring Boot ?

Khushi Vashishtha
CodeX
Published in
3 min readMay 16, 2024
  • In Spring Boot, The default name of the properties file is “application.properties”.
  • In Spring Boot applications, in general, we will provide the properties file under resources folder.
application.program inside the resources folder

Main purpose of application.properties file :

In a Spring Boot application, the application.properties file plays a crucial role in configuring various aspects of the application. It serves as a central repository for key-value pairs that define how the application behaves and interacts with its environment. Let's explore the main purposes of the properties file in Spring Boot:

1. Providing Application Logical Names

The properties file allows you to define logical names for different parts of your application. For example, you can define properties like myapp.name or myapp.version to specify the name and version of your application. These names can be used throughout your codebase to maintain consistency and make it easier to update them in the future.

2. Providing Server Configurations

You can use the properties file to configure various aspects of the embedded server used by your Spring Boot application. This includes setting the server port number (server.port), context path (server.servlet.context-path), and other server-specific settings. These configurations ensure that your application starts and runs correctly on the specified server settings.

3. Providing Database Configuration Details

One of the key functionalities of the properties file is to provide essential database configuration details. This includes properties like the datasource name (spring.datasource.name), driver class name (spring.datasource.driver-class-name), driver URL (spring.datasource.url), database username (spring.datasource.username), and database password (spring.datasource.password). These configurations are essential for connecting your application to a database and performing database operations.

4. Providing Security Configuration Details

Spring Security is a powerful framework that provides authentication, authorization, and other security features for your application. The properties file allows you to configure various security settings, such as defining roles (spring.security.user.roles), enabling/disabling security features, and specifying security filters. These configurations help secure your application and protect it from unauthorized access.

5. Providing Administrative Configuration Details

Administrative configurations, such as logging levels (logging.level.*), cache configurations, and error handling settings, can also be defined in the properties file. These configurations help you manage and monitor your application more effectively, allowing you to adjust settings as needed without modifying the code.

6. Providing Profiles Configurations

Spring Boot supports the concept of profiles, which allows you to define different sets of configurations for different environments (e.g., development, testing, production). The properties file can be used to define profile-specific configurations, such as database settings (spring.profiles.active), logging configurations, and other environment-specific settings. This makes it easy to manage and switch between different environments without changing the application code.

Here is the Example of first application using application.properties file

  1. Create a application.properties file under the resources and provide data in key value manner like the below example.
application.properties

2. Create Controller to control the properties file inside the com.kv.controller package.

Controller Program

3. Main method to execute the program inside the com.kv package

Main program with Main Method

4. Here is the output of the application with 1234 (changed port number ) port number.

Output of the above Application

Note : Create Packages and classes like the given example.

How to Create Packages and classes

Stay connected for more information about Applications.Properties and Spring Boot. . .

--

--

Khushi Vashishtha
CodeX
Writer for

CS student | Java Developer | Tech Blogger @ Medium | Sharing Java insights, tutorials, and tips.