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