通过application.yml将值动态注入到各属性中
public class Clazz {
private String str; // 字符串
private List<String> strings; // 字符串列表
private List<User> users; // 对象列表,脑补User中包含name和age两个属性
}
编写yml文件application.yml
custom:
str: ABCD
strings: ABC, DEF, GHI
users:
- { name: zhangsan, age: 20 }
- { name: lisi, age: 22 }
SpringBoot配置
@Component
@ConfigurationProperties("custom") // 或(prefix = "custom")
public class Clazz {
private String str; // 字符串
private List<String> strings; // 字符串列表
private List<User> users; // 对象列表,脑补User中包含name和age两个属性
}
引入POM configuration-processor依赖
<dependency>
<groupId> org.springframework.boot </groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>