springboot讀取yml配置綁定靜態屬性

springboot讀取yml配置綁定靜態屬性

1、yml 文件配置

wechat:
  appid: xxxxxxxxxxx

2、啓動類添加註解@EnableConfigurationProperties

@EnableConfigurationProperties({ WechatConfig.class })
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3、綁定靜態屬性的java類

@ConfigurationProperties(prefix = "wechat")
public class WechatConfig {

	public static String APPID;

	@Value("${wechat.appid}")
	public void setAPPID(String aPPID) {
		WechatConfig.APPID = aPPID;
	}
	
	public String getAPPID() {
		return APPID;
	}
}

點滴積攢,都是成長!希望以上文章能對您有所幫助。

發佈了68 篇原創文章 · 獲贊 63 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章