Springboot 如何指定獲取自己寫的配置properties文件的值
獲取yml的可以參考這篇:
Springboot 指定獲取出 yml文件里面的配置值
直接進(jìn)入正題:
先創(chuàng)建一個(gè) 配置文件test_config.properties:

test.number=123456789
接下來獲取test.number對(duì)應(yīng)的值
這里我們采取最直接的方式(也可以通過注解獲?。?,特意準(zhǔn)備了個(gè)工具類 PropertiesUtil.java :
package com.test.webflux.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
/**
* 配置文件讀取
*
* @Author: JCccc
* @Des: ElegantDay
*/
public class PropertiesUtil {
private static Logger log = LoggerFactory.getLogger(PropertiesUtil.class);
private static Properties props;
//項(xiàng)目根目錄文件夾內(nèi)讀取
// static {
// if (props == null) {
// props = new Properties();
// try {
// props.load(new FileInputStream("/testDemo/config/test_config.properties"));
// } catch (IOException e) {
// log.error("配置文件讀取異常", e);
// }
// }
// }
//resource文件夾內(nèi)讀取
static {
String fileName = "test_config.properties";
props = new Properties();
try {
props.load(new InputStreamReader(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName), "UTF-8"));
} catch (IOException e) {
log.error("配置文件讀取異常", e);
}
}
/**
* 根據(jù)配置文件中的key獲取value
* @param key
* @return
*/
public static String getProperty(String key) {
String value = props.getProperty(key.trim());
if (StringUtils.isEmpty(value)) {
return null;
}
return value.trim();
}
/**
* 根據(jù)配置文件中的key獲取value (當(dāng)獲取不到值賦予默認(rèn)值)
* @param key
* @param defaultValue
* @return
*/
public static String getProperty(String key, String defaultValue) {
String value = props.getProperty(key.trim());
if (StringUtils.isEmpty(value)) {
value = defaultValue;
}
return value.trim();
}
public static void main(String[] args) {
System.out.println("配置文件中有key&value:"+PropertiesUtil.getProperty("test.number"));
System.out.println("配置文件無有key&value,賦予默認(rèn)值"+PropertiesUtil.getProperty("test.numberNone","默認(rèn)值 JCccc"));
}
}
OK,測(cè)試下工具類的main方法:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
JAVA操作MongoDB數(shù)據(jù)庫(kù)實(shí)例教程
MongoDB是一個(gè)文檔型數(shù)據(jù)庫(kù),是NOSQL家族中最重要的成員之一,下面這篇文章主要給大家介紹了關(guān)于JAVA操作MongoDB數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05
springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題
這篇文章主要介紹了springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
java Aop實(shí)現(xiàn)自動(dòng)填充字段值示例
這篇文章主要為大家介紹了Aop實(shí)現(xiàn)自動(dòng)填充字段值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
解決執(zhí)行Junit單元測(cè)試報(bào)錯(cuò)java.lang.ClassNotFoundException問題
這篇文章主要介紹了解決執(zhí)行Junit單元測(cè)試報(bào)錯(cuò)java.lang.ClassNotFoundException問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot結(jié)合Redis實(shí)現(xiàn)緩存
本文主要介紹了SpringBoot結(jié)合Redis實(shí)現(xiàn)緩存,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析
這篇文章主要介紹了Netty組件NioEventLoopGroup創(chuàng)建線程執(zhí)行器源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Mybatis實(shí)現(xiàn)分頁的注意點(diǎn)
Mybatis提供了強(qiáng)大的分頁攔截實(shí)現(xiàn),可以完美的實(shí)現(xiàn)分功能。下面小編給大家分享小編在使用攔截器給mybatis進(jìn)行分頁所遇到的問題及注意點(diǎn),需要的朋友一起看看吧2017-07-07

