分两步:设置yaml,注入参数

1.设置yaml,文件名:application.yml

  1. runcommand:
  2. firstMapList:
  3. key11:
  4. - k1
  5. - k2
  6. - k3
  7. key22:
  8. - k1
  9. - k2
  10. - k3
  11. list:
  12. - lk1
  13. - lk2
  14. - lk3
  15. maps: {key1: 'value1', key2: 'value2'}
  16. item:
  17. key1: 1
  18. key2: 2
  19. items:
  20. - key1: 1
  21. key2: 2
  22. - key1: 3
  23. key2: 4
  24. list-array: ["A","B"]

2.代码注入参数

  1. @Component
  2. @Data
  3. @Configuration
  4. @PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8")
  5. @ConfigurationProperties(prefix = "runcommand")
  6. public class StartCommandConfig {
  7. private List<String> listArray;
  8. private List<String> list;
  9. private Map<String, String> maps;
  10. private Map<String, List<String>> firstMapList;
  11. private Item item;
  12. private List<Item> items;
  13. @Data
  14. public static class Item {
  15. private int key1;
  16. private int key2;
  17. }
  18. }

3.输出结果

  1. System.out.println(startCommandConfig.getFirstMapList());
  2. System.out.println(startCommandConfig.getList());
  3. System.out.println(startCommandConfig.getMaps());
  4. System.out.println(startCommandConfig.getListArray());
  5. {key11=[k1, k2, k3], key22=[k1, k2, k3]}
  6. [lk1, lk2, lk3]
  7. {key1=value1, key2=value2}
  8. ["A", "B"]