分两步:设置yaml,注入参数
1.设置yaml,文件名:application.yml
runcommand:
firstMapList:
key11:
- k1
- k2
- k3
key22:
- k1
- k2
- k3
list:
- lk1
- lk2
- lk3
maps: {key1: 'value1', key2: 'value2'}
item:
key1: 1
key2: 2
items:
- key1: 1
key2: 2
- key1: 3
key2: 4
list-array: ["A","B"]
2.代码注入参数
@Component
@Data
@Configuration
@PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8")
@ConfigurationProperties(prefix = "runcommand")
public class StartCommandConfig {
private List<String> listArray;
private List<String> list;
private Map<String, String> maps;
private Map<String, List<String>> firstMapList;
private Item item;
private List<Item> items;
@Data
public static class Item {
private int key1;
private int key2;
}
}
3.输出结果
System.out.println(startCommandConfig.getFirstMapList());
System.out.println(startCommandConfig.getList());
System.out.println(startCommandConfig.getMaps());
System.out.println(startCommandConfig.getListArray());
{key11=[k1, k2, k3], key22=[k1, k2, k3]}
[lk1, lk2, lk3]
{key1=value1, key2=value2}
["A", "B"]