Java如何使用ConfigurationProperties獲取yml中的配置
使用ConfigurationProperties獲取yml的配置
我們在開發(fā)過程中,會經(jīng)常遇到需要自定義配置的場景,比如配置一個ip,一個地址等,并將其寫入到y(tǒng)ml文件中,在項目中使用@Value("${xxxx.xxxx}")來獲取自定義的配置,其實是這樣是有些笨重的,每定義一個配置,都需要寫一個@Value來獲取,那為啥不使用一個java config來統(tǒng)一獲取配置呢?
使用方法
編寫yml配置文件
user:
config:
# user_name user-name userName這三種配置方式都可以被識別到
user_name: "zhangsan"
age: "20"
exmail: "123@123.com"
address: "火星"編寫Java config類
// 需要重寫get與set方法,此處使用lombok注解來代替
@Data
// 實例化到spring容器中
@Component
// 獲取前綴為user.config的配置信息,與該類下的屬性對比,進行綁定
@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 {
/**
* 項目名稱
*/
private static String name;
/**
* 版本
*/
private String version;
/**
* 版權(quán)年份
*/
private String copyrightYear;
/**
* 實例演示開關(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對應(yīng)的ser方法也定義為了靜態(tài)方法。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中分頁插件PageHelper的使用詳解
分頁查詢是為了高效展示大量數(shù)據(jù),通過分頁將數(shù)據(jù)劃分為多個部分逐頁展示,原生方法需手動計算數(shù)據(jù)起始行,而使用PageHelper插件則簡化這一過程,本文給大家介紹SpringBoot中分頁插件PageHelper的使用,感興趣的朋友一起看看吧2024-09-09
詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實用
這篇文章主要介紹了詳解SpringMVC和MyBatis框架開發(fā)環(huán)境搭建和簡單實用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Java使用Scanner類進行控制臺輸入實現(xiàn)方法
這篇文章主要介紹了Java使用Scanner類進行控制臺輸入實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
SpringBoot枚舉類型參數(shù)認證的實現(xiàn)代碼
項目當(dāng)中經(jīng)常需要接口參數(shù)是否在一個可選的范圍內(nèi),也就是驗證類枚舉參數(shù)的需求,所以本文我們將使用SpringBoot實現(xiàn)枚舉類型參數(shù)認證,文中有詳細的代碼示例,需要的朋友可以參考下2023-12-12

