When comings to Spring Boot development, in some cases, we need to read some configuration information from properties files. In this post, we will show how to read configuration information from the YAML file.

YAML

YAML is a superset of JSON and is a convenient format for specifying hierarchical configuration data. The SpringApplication class automatically supports YAML as an alternative to properties whenever you have the SnakeYAML library on your classpath.

SnakeYAML is automatically provided by spring-boot-starter.

Spring Framework with two classes to load YAML files. The YamlPropertiesFactoryBean loads YAML as Properties and the YamlMapFactoryBean loads YAML as a Map.

For example, with a YAML file:

environments:
    dev:
        url: https://dev.etbye.com
        name: Developer Setup
    prod:
        url: https://another.etbye.com
        name: My App

Then the YAML file would be transformed into the following properties:

 environments.dev.url=https://dev.etbye.com
 environments.dev.name=Developer Setup
 environments.prod.url=https://another.etbye.com
 environments.prod.name=My App

To bind to YAML properties by using Spring Boot’s Binder utilities @ConfigurationProperties, like this:

@ConfigurationProperties(prefix = "library")
public class LibraryProerties {
   ...
}

A demo to read YAML file

First, we create a YAML file named as application.yml in the OurProjectName/config/ directory.

application.yml

library:
  location: Etbye Spring Boot website
  books:
    - name: Andvanced Spring Boot
      description: This guide walks you through the process of creating an application that accesses...
    - name: REST services with Spring
      description: REST has quickly become the de-facto standard for building web services on the web because they’re easy to build and easy to consume.

Through@ConfigurationPropertiesreading and binding to the bean.

We create an entity class LibraryProerties, and add the @Component annotation. It can be like a normal bean as injected into the class to use.

package com.etbye.entity;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConfigurationProperties(prefix = "library")
@Setter
@Getter
@ToString
public class LibraryProerties {
    private String location;
    private List<Book> books;

    @Setter
    @Getter
    @ToString
    static class Book {
        String name;
        String description;
    }
}

Remember to add the dependency of Lombok to the pom.xml

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

Then, we can use it as a common bean.

package com.etbye;

import com.etbye.entity.LibraryProerties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication implements InitializingBean {

    private final LibraryProerties library;

    public DemoApplication(LibraryProerties library) {
        this.library = library;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Location: " + library.getLocation());
        System.out.println("Books: " + library.getBooks());
    }
}

Run the project, the output like that:

Location: Etbye Spring Boot website
Books: [LibraryProerties.Book(name=Andvanced Spring Boot, description=This guide walks you through the process of creating an application that accesses...), LibraryProerties.Book(name=REST services with Spring, description=REST has quickly become the de-facto standard for building web services on the web because they’re easy to build and easy to consume.)]

Reference: Using YAML Instead of Properties