yml注入map

记录一次yml文件注入Map,首先是配置类:

@Component@Configuration@PropertySource(value = {"classpath:/application.yml"}, encoding = "utf-8")@ConfigurationProperties(prefix = "serious")@Datapublic class SeriousConfig {    /**     * 嚴重程度 低     */    private Map<String, String> mapLow;    /**     * 嚴重程度 中     */    private Map<String, String> mapMiddle;    /**     * 嚴重程度 高     */    private Map<String, String> mapHigh;}

然后是yml文件:

serious:  mapLow:    label: 低    value: 0  mapMiddle:    label: 中    value: 1  mapHigh:    label: 高    value: 2

最后是实际的方法调用:

public List<Map<String, String>> getSerious() {        List<Map<String, String>> list = new ArrayList<>();        Map<String, String> lowMap = tools.seriousConfig.getMapLow();        Map<String, String> middleMap = tools.seriousConfig.getMapMiddle();        Map<String, String> highMap = tools.seriousConfig.getMapHigh();        list.add(lowMap);        list.add(middleMap);        list.add(highMap);        return list;    }
(0)

相关推荐