Master Spring Boot YAML: Configure Complex Objects with Ease

This article explains how to use Spring Boot's YAML configuration files to define and manage simple properties, collections, maps, nested objects, and complex data structures, providing code examples and a complete sample to help developers efficiently handle intricate configuration scenarios.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot YAML: Configure Complex Objects with Ease

Environment: Spring Boot 2.7.12

1. Introduction

In Spring Boot, configuration files (.yml, .yaml, or .properties) define various application parameters. When using YAML, you can structure complex objects and settings in a readable way. This article explores how to configure complex objects using .yml files.

2. Required Dependency

<dependencies>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
  </dependency>
</dependencies>

The spring-boot-configuration-processor dependency enables IDE assistance for custom configuration properties in YAML.

3. Configuration Methods

3.1 Simple Property Configuration

@Component
@ConfigurationProperties(prefix = "pack")
public class ConfigProperties {
  private String name;
  private Integer count;
  private Boolean enable;
}

Corresponding YAML:

pack:
  count: 1
  enable: true
  name: 测试

3.2 List and Map Collections

List

Method 1:

private List<String> categorys = new ArrayList<>();
pack:
  categorys:
  - 足球
  - 篮球

Method 2:

pack:
  categorys: 足球, 篮球

Method 3:

pack:
  categorys: [足球, 篮球]

Map

Method 1:

private Map<String, String> params = new HashMap<>();
pack:
  params:
    p1: v1
    p2: v2
    p3: v3

Method 2:

pack:
  params: {p1: v1, p2: v2, p3: v3}

3.3 Nested Objects

public class Person {
  private String name;
  private Integer age;
}
private Person person;

YAML:

pack:
  person:
    age: 10
    name: 张三

3.4 Collections of Objects

List of Objects

Method 1:

private List<Person> persons = new ArrayList<>();
pack:
  persons:
  - age: 20
    name: 田七
  - age: 30
    name: 王五

Method 2:

pack:
  persons:
  - {name: 田七, age: 20}
  - {name: 王五, age: 30}

Map of Objects

Method 1:

private Map<String, Person> mappings = new HashMap<>();
pack:
  mappings:
    m1:
      age: 40
      name: 甜甜
    m2:
      age: 50
      name: 莉莉

Method 2:

pack:
  mappings:
    m1: {name: 田七, age: 20}
    m2: {name: 王五, age: 30}

Method 3:

pack:
  mappings: {m1: {name: 田七, age: 20}, m2: {name: 王五, age: 30}}

3.5 Complete Example

@Component
@ConfigurationProperties(prefix = "pack")
public class ConfigProperties {
  private String name;
  private Integer count;
  private Boolean enable;
  private List<String> categorys = new ArrayList<>();
  private List<Person> persons = new ArrayList<>();
  private Map<String, String> params = new HashMap<>();
  private Map<String, Person> mappings = new HashMap<>();
  private Person person;
}

YAML:

pack:
  count: 1
  enable: true
  name: 测试
  categorys: [足球, 篮球]
  params: {p1: v1, p2: v2, p3: v3}
  person:
    age: 10
    name: 张三
  persons:
  - {name: 田七, age: 20}
  - {name: 王五, age: 30}
  mappings: {m1: {name: 田七, age: 20}, m2: {name: 王五, age: 30}}

Result screenshot:

Result
Result

Additional illustration:

Diagram
Diagram

End of tutorial.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javabackend-developmentconfigurationspring-bootYAML
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.