Spring Boot项目—— yml 配置map

背景

  • 一次需要在yml配置文件中,需要配置Map格式的数据。

配置

  • yml文件配置如下
dingtalk:
  farm-notify:
    farm-0001: https://oapi.dingtalk.com/robot/send?access_token=678b352043933dd7f2cbd09520e32a72d27b2005e23897dbb9b23c2524de122f
  • properties属性获取
@Component
@ConfigurationProperties("dingtalk")
public class DingTalkProperties {
    private Map<String,String> farmNotify; //<farmCode,url>

    DingTalkProperties() {
    }

    public Map<String, String> getFarmNotify() {
        return farmNotify;
    }

    public void setFarmNotify(Map<String, String> farmNotify) {
        this.farmNotify = farmNotify;
    }

    @Override
    public String toString() {
        return "DingTalkProperties{" +
                "farmNotify=" + farmNotify +
                '}';
    }
}

扩展

  • yml文件中使用farm-notify破折号,但我们类中的属性名称称为farmNotify带有破折号的 YAML 属性将自动转换为代码中等效的驼峰式大小写。
posted @ 2021-06-30 19:24  话·醉月  阅读(10139)  评论(0编辑  收藏  举报