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 屬性將自動轉換為代碼中等效的駝峰式大小寫。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM