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)文章
詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn)
這篇文章主要介紹了詳解Java Fibonacci Search斐波那契搜索算法代碼實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Java的Tomcat和Servlet的運(yùn)行原理詳解
這篇文章主要為大家詳細(xì)介紹了Java的Tomcat和Servlet,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
java Spring Boot 配置redis pom文件操作
這篇文章主要介紹了java Spring Boot 配置redis pom文件操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07
SpringBoot過濾器實(shí)現(xiàn)項(xiàng)目內(nèi)接口過濾詳解
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何利用過濾器實(shí)現(xiàn)項(xiàng)目內(nèi)接口過濾,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-04-04
詳解使用spring aop實(shí)現(xiàn)業(yè)務(wù)層mysql 讀寫分離
本篇文章主要介紹了使用spring aop實(shí)現(xiàn)業(yè)務(wù)層mysql 讀寫分離,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
使用Post方法模擬登陸爬取網(wǎng)頁的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄褂肞ost方法模擬登陸爬取網(wǎng)頁的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03

