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.yml/application.yaml File

The application.yml (or its equivalent application.yaml) file in Spring Boot is an alternative to the traditional application.properties file for configuration purposes. YAML, which stands for "YAML Ain't Markup Language," is a human-readable data serialization format. Many developers prefer YAML over properties format because of its clear hierarchical structure, which is particularly useful when dealing with complex configurations.

Basic Usage:

In a Spring Boot application, instead of creating an application.properties file, you can create an application.yml file in the src/main/resources directory.

Here are some common configurations in YAML format:

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

Accessing Properties in Code:

Just like with application.properties, you can access these properties in your Spring Beans using the @Value annotation or by binding them to configuration classes with @ConfigurationProperties.

Profiles:

Spring Boot also supports profile-specific configurations in YAML. There are a couple of ways to define profile-specific configurations within the application.yml:

  • Using Document Separators:
server:
  port: 8080

---
spring:
  profiles: dev
server:
  port: 8081

---
spring:
  profiles: prod
server:
  port: 8082
  • Nested under the Profile Name:
server:
  port: 8080
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
server:
  port: 8081

---
spring:
  profiles: prod
server:
  port: 8082

Including Properties:

If you want to include another YAML file's properties, you can use the spring.config.import property:

spring:
  config:
    import: classpath:another-config.yml

Advantages of using YAML:

  • Hierarchical Representation: Unlike properties files, YAML files naturally represent hierarchical configurations, making them more readable.

  • Support for Complex Data Structures: It's easier to represent arrays, lists, and maps in YAML.

  • Profile-specific Configurations: As demonstrated above, it's convenient to have multiple profile configurations in one file separated by document breaks (---).

Conclusion:

Both application.properties and application.yml have their places in Spring Boot applications. Your choice depends on your preference and the complexity of the configurations. However, as configurations grow, the hierarchical format of YAML can be more readable and easier to manage than the flat properties format.

  1. Using YAML configuration in Spring Boot:

    • YAML is a human-readable configuration format.
    • Example:
      # YAML configuration example
      server:
        port: 8080
      
      spring:
        datasource:
          url: jdbc:mysql://localhost:3306/mydatabase
          username: root
          password: password
      
  2. Common properties in Spring Boot application.yml:

    • Define common properties for various configurations.
    • Example:
      # Common properties in application.yml
      server:
        port: 8080
      
      spring:
        datasource:
          url: jdbc:mysql://localhost:3306/mydatabase
          username: root
          password: password
      
  3. Profiles and application.yml in Spring Boot:

    • Use profiles to specify configuration for different environments.
    • Example:
      # Default properties
      server:
        port: 8080
      
      spring:
        datasource:
          url: jdbc:mysql://localhost:3306/defaultdb
      
      # Properties for the development profile
      ---
      spring:
        profiles: dev
        datasource:
          url: jdbc:mysql://localhost:3306/devdb
      
  4. Nesting and structuring YAML in Spring Boot:

    • Nest properties for structured configuration.
    • Example:
      # Nested and structured YAML configuration
      myapp:
        server:
          port: 8080
      
        database:
          url: jdbc:mysql://localhost:3306/mydatabase
          username: root
          password: password
      
  5. Spring Boot YAML array configuration:

    • Define arrays of values in YAML.
    • Example:
      # YAML array configuration
      fruits:
        - apple
        - orange
        - banana
      
  6. Conditional properties in application.yml:

    • Use conditional properties based on profiles or conditions.
    • Example:
      # Conditional YAML configuration
      server:
        port: 8080
      
      spring:
        profiles:
          active: dev
      
  7. Overriding default settings with application.yml:

    • Override default settings using application-specific YAML.
    • Example:
      # Override default server port
      server:
        port: 9090
      
  8. Security considerations for application.yml in Spring Boot:

    • Avoid storing sensitive information in plain text.
    • Use encryption or secure vaults for sensitive data.
    • Example (encrypted property):
      # Encrypted property
      db:
        password: ENC(encrypted_password)