Java如何使用ConfigurationProperties獲取yml中的配置
使用ConfigurationProperties獲取yml的配置
我們在開發(fā)過程中,會(huì)經(jīng)常遇到需要自定義配置的場景,比如配置一個(gè)ip,一個(gè)地址等,并將其寫入到y(tǒng)ml文件中,在項(xiàng)目中使用@Value("${xxxx.xxxx}")來獲取自定義的配置,其實(shí)是這樣是有些笨重的,每定義一個(gè)配置,都需要寫一個(gè)@Value來獲取,那為啥不使用一個(gè)java config來統(tǒng)一獲取配置呢?
使用方法
編寫yml配置文件
user: config: # user_name user-name userName這三種配置方式都可以被識(shí)別到 user_name: "zhangsan" age: "20" exmail: "123@123.com" address: "火星"
編寫Java config類
// 需要重寫get與set方法,此處使用lombok注解來代替 @Data // 實(shí)例化到spring容器中 @Component // 獲取前綴為user.config的配置信息,與該類下的屬性對(duì)比,進(jìn)行綁定 @ConfigurationProperties(prefix = "user.config") public class UserConfig { private String userName; private String age; private String exmail; private String address; }
在需要使用的地方注入
@Resource private UserConfig userConfig;
測試
@ConfigurationProperties獲取不到配置文件屬性值問題
application.yml
配置類
@Component @ConfigurationProperties(prefix = "system") public class SystemConfig { /** * 項(xiàng)目名稱 */ private static String name; /** * 版本 */ private String version; /** * 版權(quán)年份 */ private String copyrightYear; /** * 實(shí)例演示開關(guān) */ private boolean demoEnabled; /** * windows環(huán)境下,文件上傳路徑(本地上傳) */ private static String winUploadPath; /** * 其他系統(tǒng)環(huán)境(linux、Mac...)環(huán)境下,文件上傳路徑(本地上傳) */ private static String otherUploadPath; /** * 獲取地址開關(guān) */ private static boolean addressEnabled; public static String getName() { return name; } public void setName(String name) { SystemConfig.name = name; } public String getVersion() { return version; } public void setVersion(String version) { this.version = version; } public String getCopyrightYear() { return copyrightYear; } public void setCopyrightYear(String copyrightYear) { this.copyrightYear = copyrightYear; } public boolean isDemoEnabled() { return demoEnabled; } public void setDemoEnabled(boolean demoEnabled) { this.demoEnabled = demoEnabled; } public static String getWinUploadPath() { return winUploadPath; } public static void setWinUploadPath(String winUploadPath) { SystemConfig.winUploadPath = winUploadPath; } public static String getOtherUploadPath() { return otherUploadPath; } public static void setOtherUploadPath(String otherUploadPath) { SystemConfig.otherUploadPath = otherUploadPath; } public static boolean isAddressEnabled() { return addressEnabled; } public void setAddressEnabled(boolean addressEnabled) { SystemConfig.addressEnabled = addressEnabled; } /** * 判斷當(dāng)前操作系統(tǒng),返回相應(yīng)的本地上傳路徑 * * @return String * @author Liangyixiang * @date 2021/11/15 **/ public static String getUploadPath() { OsInfo osInfo = SystemUtil.getOsInfo(); // 判斷系統(tǒng)環(huán)境獲取上傳路徑 if(ObjectUtils.isNotEmpty(osInfo) && osInfo.isWindows()){ return getWinUploadPath(); }else{ return getOtherUploadPath(); } } /** * 獲取業(yè)務(wù)系統(tǒng)名稱 */ public static String getSystemName() { return getName(); } }
name、addressEnabled 以及 winUploadPath、otherUploadPath 都是靜態(tài)的成員變量,但是他們name、addressEnabled能獲取到配置文件的值,winUploadPath、otherUploadPath不可以。
原因就是
winUploadPath、otherUploadPath對(duì)應(yīng)的ser方法也定義為了靜態(tài)方法。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot項(xiàng)目中的視圖解析器問題(兩種)
SpringBoot官網(wǎng)推薦使用HTML視圖解析器,但是根據(jù)個(gè)人的具體業(yè)務(wù)也有可能使用到JSP視圖解析器,所以本文介紹了兩種視圖解析器,感興趣的可以了解下2020-06-06SpringBoot中分頁插件PageHelper的使用詳解
分頁查詢是為了高效展示大量數(shù)據(jù),通過分頁將數(shù)據(jù)劃分為多個(gè)部分逐頁展示,原生方法需手動(dòng)計(jì)算數(shù)據(jù)起始行,而使用PageHelper插件則簡化這一過程,本文給大家介紹SpringBoot中分頁插件PageHelper的使用,感興趣的朋友一起看看吧2024-09-09詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實(shí)用
這篇文章主要介紹了詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實(shí)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Java使用Scanner類進(jìn)行控制臺(tái)輸入實(shí)現(xiàn)方法
這篇文章主要介紹了Java使用Scanner類進(jìn)行控制臺(tái)輸入實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Java實(shí)現(xiàn)微信紅包分配規(guī)則
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)仿微信紅包分配規(guī)則,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-02-02SpringBoot枚舉類型參數(shù)認(rèn)證的實(shí)現(xiàn)代碼
項(xiàng)目當(dāng)中經(jīng)常需要接口參數(shù)是否在一個(gè)可選的范圍內(nèi),也就是驗(yàn)證類枚舉參數(shù)的需求,所以本文我們將使用SpringBoot實(shí)現(xiàn)枚舉類型參數(shù)認(rèn)證,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-12-12