您好,登錄后才能下訂單哦!
這篇文章主要介紹“Java怎么使用ConfigurationProperties獲取yml中的配置”的相關(guān)知識(shí),小編通過實(shí)際案例向大家展示操作過程,操作方法簡單快捷,實(shí)用性強(qiáng),希望這篇“Java怎么使用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;
測試
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)方法。
關(guān)于“Java怎么使用ConfigurationProperties獲取yml中的配置”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí),可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會(huì)為大家更新不同的知識(shí)點(diǎn)。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。