What is application.properties file in spring boot

In a Spring Boot application, application.properties is a configuration file used to specify application settings and properties. This file is part of Spring Boot’s externalized configuration system, which allows you to configure various aspects of your application without modifying the code. The application.properties file is typically located in the src/main/resources directory of your project.

Key Features of application.properties

  1. Centralized Configuration:
  • It serves as a central place to define configuration properties for the application, such as database connection details, server settings, logging levels, and more.
  1. Property Key-Value Pairs:
  • The file contains key-value pairs, where each key represents a specific configuration property and its corresponding value sets the configuration. Example:
   server.port=8080
   spring.datasource.url=jdbc:mysql://localhost:3306/mydb
   spring.datasource.username=root
   spring.datasource.password=secret
   logging.level.org.springframework=INFO
  1. Overriding Defaults:
  • Spring Boot provides default configurations for many aspects of the application. The application.properties file allows you to override these defaults with custom values.
  1. Profile-Specific Configurations:
  • You can define environment-specific configurations using Spring Profiles. For example, you can have different application.properties files for different profiles like dev, test, and prod.
  • These files are named application-{profile}.properties (e.g., application-dev.properties), and you can activate a profile using the spring.profiles.active property. Example for Profiles:
   # application-dev.properties
   server.port=8081
   spring.datasource.url=jdbc:mysql://localhost:3306/devdb
   # application-prod.properties
   server.port=8080
   spring.datasource.url=jdbc:mysql://localhost:3306/proddb

Activating a Profile:

   spring.profiles.active=dev
  1. Environment Variable Substitution:
  • Spring Boot allows you to use environment variables or system properties in application.properties by using placeholders. Example:
   spring.datasource.password=${DB_PASSWORD}
  1. Custom Properties:
  • You can define custom properties and use them in your application code. For example, if you define a property myapp.custom.property=value, you can access it using @Value("${myapp.custom.property}") in your Spring beans. Example:
   myapp.custom.property=HelloWorld

Accessing in Code:

   @Component
   public class MyComponent {
       @Value("${myapp.custom.property}")
       private String customProperty;

       public void printProperty() {
           System.out.println(customProperty); // Outputs: HelloWorld
       }
   }
  1. Integration with Other Configuration Sources:
  • Spring Boot supports multiple configuration sources, such as YAML files (application.yml), environment variables, command-line arguments, and configuration servers. Properties in application.properties can be overridden by these sources based on their precedence.

Example Configuration

Here’s a simple example of application.properties for a Spring Boot application:

# Server Configuration
server.port=8080
server.servlet.context-path=/myapp

# DataSource Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password

# Logging Configuration
logging.level.org.springframework.web=DEBUG

# Custom Property
myapp.feature.enabled=true

Summary

  • Purpose: application.properties is used to define and override configuration properties for a Spring Boot application.
  • Usage: It simplifies configuration management and supports profile-specific settings, environment variables, and custom properties.
  • Flexibility: Provides a central place to configure various aspects of your application, with support for different configuration sources and profiles.

By using application.properties, you can easily manage application configurations and ensure that your application behaves correctly across different environments.

Author: Susheel kumar

Leave a Reply

Your email address will not be published. Required fields are marked *