1. Overview

In this quick tutorial, we’re going to take a close look at how to inject a map from a YAML file in Spring Boot.

First, we’ll start with a little bit of insight on YAML files in Spring Framework. Then, we’ll showcase, through a practical example, how to bind YAML properties to a Map.

2. YAML Files in Spring Framework

Using YAML files to store external configuration data is a common practice among Spring developers. Basically, Spring supports YAML documents as an alternative to properties and uses SnakeYAML under the hood to parse them.

Without further ado, let’s see what a typical YAML file looks like:

server:
  port: 8090
  application:
    name: myapplication
    url: http://myapplication.com

As we can see, the YAML file is self-explanatory and more human-readable. As a matter of fact, YAML provides a fancy and concise way to store hierarchical configuration data.

By default, Spring Boot reads configuration properties from application.properties or application.yml at application startup. However, we can use @PropertySource to load a custom YAML file.

Now that we are familiar with what a YAML file is, let’s see how to inject YAML properties as a Map in Spring Boot.

#spring-boot #yaml #java #developer

How to Inject a Map from a YAML File in Spring Boot
118.40 GEEK