Spring Boot Tutorial

Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)

Prerequisite (Spring Core Concepts)

Spring Boot Core

Spring Boot with REST API

Spring Boot with Database and Data JPA

Spring Boot with Kafka

Spring Boot with AOP

Spring Boot - Application Properties

In Spring Boot, application.properties is a central place to externalize configuration. Instead of hardcoding values in your application code, you can define properties in this file, which allows easier configuration, maintenance, and environment-specific adjustments.

Location:

By default, Spring Boot looks for application.properties in the following locations:

  • A /config subdirectory of the current directory.
  • The current directory.
  • A classpath /config package.
  • The root of the classpath.

The list is ordered by precedence. Properties defined in locations higher on the list will override those defined further down.

Basic Usage:

Here are some common configurations and their respective properties:

  • Server Port:
server.port=8081
  • Database Configuration (for example, for MySQL with Spring Data JPA):
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
  • Logging Level:
logging.level.org.springframework=INFO
logging.level.com.example=DEBUG
  • Custom Properties: You can define your custom properties and later inject them into your Spring Beans.
app.custom.property=value

Accessing Properties in Code:

You can access these properties in your Spring Beans by using the @Value annotation or by binding them to configuration classes using @ConfigurationProperties.

Using @Value:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

    @Value("${app.custom.property}")
    private String customProperty;

    // ...
}

Using @ConfigurationProperties:

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {

    private String customProperty;

    // Getters and setters...

}

In application.properties:

app.custom.property=value

Profiles:

Spring Boot allows you to define properties specific to a profile. Profiles are configurations specific to a particular environment (e.g., dev, prod, test).

To use profiles, you can create properties files like:

  • application-dev.properties
  • application-prod.properties

You can activate a profile using:

spring.profiles.active=dev

or via a JVM argument:

-Dspring.profiles.active=dev

YAML Format:

Instead of .properties, you can also use .yml or .yaml for your configuration. The application.yml would be an equivalent YAML-based configuration file.

Conclusion:

application.properties (or its YAML counterpart application.yml) in Spring Boot provides a powerful way to manage application configurations. By externalizing configurations, you can ensure your application remains flexible and maintainable.

  1. Common Spring Boot application properties:

    • Spring Boot provides a wide range of common application properties.
    • Examples:
      # Server settings
      server.port=8080
      
      # Database configuration
      spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
      spring.datasource.username=root
      spring.datasource.password=password
      
  2. Externalizing configuration in Spring Boot:

    • Externalize configuration to separate files or directories.
    • Example:
      # External configuration file (e.g., application.properties)
      spring.config.location=classpath:/config/
      
      # External directory containing configuration files
      spring.config.name=myapplication
      
  3. Profiles in Spring Boot application.properties:

    • Define profiles for different deployment environments.
    • Example:
      # Default properties
      spring.datasource.url=jdbc:mysql://localhost:3306/defaultdb
      
      # Properties for the development profile
      spring.profiles.active=dev
      spring.datasource.url=jdbc:mysql://localhost:3306/devdb
      
  4. Property file hierarchy in Spring Boot:

    • Spring Boot follows a specific order when loading property files.
    • Hierarchy: application.properties (default) < application-{profile}.properties < application-{profile}.yml.
    • Example:
      # Default properties
      spring.datasource.url=jdbc:mysql://localhost:3306/defaultdb
      
  5. Dynamic configuration with Spring Boot application.properties:

    • Use placeholders for dynamic configuration.
    • Example:
      # Dynamic configuration using placeholders
      app.name=MyApp
      welcome.message=Welcome to ${app.name}!
      
  6. Using YAML for Spring Boot application properties:

    • YAML is an alternative to properties files for configuration.
    • Example:
      # YAML configuration
      server:
        port: 8080
      
      spring:
        datasource:
          url: jdbc:mysql://localhost:3306/mydatabase
          username: root
          password: password
      
  7. Encryption and decryption of properties in Spring Boot:

    • Encrypt sensitive properties for security.
    • Use jasypt or other encryption libraries.
    • Example:
      # Encrypted property
      db.password=ENC(encrypted_password)
      
  8. Overriding default properties in Spring Boot:

    • Override default properties by providing your own.
    • Example:
      # Override default server port
      server.port=9090