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.
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: v3Method 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:
Additional illustration:
End of tutorial.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
