SpringBoot 框架中yml文件自定义常量及使用

yml文件:

dingtalk:
  DingTalkAccesssToken: access_token=ca20ab9446a9fe0ea1fae59999999
  DingTalkUrl: https://testDingtalk/send?

对应java配置类:

package com.talk.util;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by HANJUN on 2021/9/10 10:24
 */
@Component
@ConfigurationProperties(prefix = "dingtalk")
public class DingTalkSetting {
    public static String DingTalkUrl;

    public  String getDingTalkUrl() {
        return DingTalkUrl;
    }

    public  void setDingTalkUrl(String dingTalkUrl) {
        DingTalkUrl = dingTalkUrl;
    }

    public static String DingTalkAccesssToken;

    public String getDingTalkAccesssToken() {
        return DingTalkAccesssToken;
    }

    public void setDingTalkAccesssToken(String dingTalkAccesssToken) {
        DingTalkAccesssToken = dingTalkAccesssToken;
    }
}

 这样就可以直接用类名.方法名来获取值了,如:

String url=DingTalkSetting.DingTalkUrl;

值得注意点:

1、yml文件中层级要分清,缩进是空格而不是tab

2、自定义配置与java类中属性一致,prefix中只能小写 

3、set方法必须为public且不能用静态static修饰

上一篇:【游戏面包屑】文件路径遍历寻找目标文件


下一篇:spring配置yml提示