Warning: Undefined array key "amp-addthis" in /home/tgagmvup/onlinestudy.guru/wp-content/plugins/addthis/backend/AddThisSharingButtonsFeature.php on line 101
lang="en-US"> What is application.properties file in spring boot - onlinestudy.guru
Site icon onlinestudy.guru

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:
  1. Property Key-Value Pairs:
   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:
  1. Profile-Specific Configurations:
   # 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.datasource.password=${DB_PASSWORD}
  1. Custom Properties:
   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:

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

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

Exit mobile version