使用Properties讀取配置文件的示例詳解
1. 配置文件信息
1.1 項(xiàng)目默認(rèn)配置文件
開發(fā)SpringBoot項(xiàng)目時(shí),使用配置文件配置項(xiàng)目相關(guān)屬性是必不可少的,SpringBoot也為我們提供了一些默認(rèn)的配置文件格式。
application.properties,以鍵值對的方式配置屬性值application.yml:以數(shù)據(jù)對象的方式配置屬性值
配置文件存放位置:src/resource/
按照SpringBoot給定的格式和位置創(chuàng)建文件并定義相關(guān)屬性值后,SpringBoot項(xiàng)目會(huì)在啟動(dòng)時(shí)自動(dòng)加載屬性值。
1.2 自定義配置文件
除了官方指定的配置文件,我們還可以自定義一個(gè)配置文件,并在合適的時(shí)候使用其中的配置信息,如db.properties、jdbc.properties、mybatis.properties。
但是由于SpringBoot對application.properties配置文件的自動(dòng)加載,大部分的屬性定義都可以在主配置文件中完成了,并使用@value等方法獲取配置內(nèi)容。
但是如果我們需要將一系列相關(guān)配置放在一個(gè)單獨(dú)的.properties文件中,這種情況下如何在項(xiàng)目中加載配置文件并讀取其中內(nèi)容呢?這個(gè)時(shí)候就需要Properties出場了。
2. Properties
2.1 認(rèn)識(shí)Properties類
Properties類對應(yīng)SpringBoot中的application.properties文件,定義為一系列屬性的集合,可以從流中讀取屬性或?qū)傩员4娴搅髦?,且屬性中值都是字符串格式?/p>
Properties類作為一個(gè)集合,繼承了Hashaable類,因此是線程安全的。
class Properties extends Hashtable<Object,Object>{
? ...
? ?public Properties() {
? ? ? ?this(null);
? }
? ...
}Properties初始化實(shí)例時(shí)也比較簡單,只需要使用new關(guān)鍵字調(diào)用類的無參構(gòu)造方法創(chuàng)建對象即可。
Properties properties = new Properties();
2.2 讀取流
Properties類中提供了load()方法來讀取流中的內(nèi)容,創(chuàng)建對象后,調(diào)用對象的load()方法,并提供字節(jié)流作為參數(shù),就可以讀取字節(jié)中屬性內(nèi)容。
簡要的讀取流程可以描述為:
- 創(chuàng)建Properties類對象
- 使用對象的load()方法讀取流文件
- 對象中讀取屬性數(shù)據(jù)后,使用getProperty()方法獲取屬性值
根據(jù)方法參數(shù)的不同,Properties中定義了兩個(gè)同名的load()方法,兩種方法本質(zhì)上都是調(diào)用了內(nèi)部的load0()方法,用來從字節(jié)流中獲取數(shù)據(jù)內(nèi)容。
//以Reader對象作為參數(shù)讀取
public synchronized void load(Reader reader) throws IOException {
? ?load0(new LineReader(reader));
}
?
//以InputStream作為參數(shù)讀取
public synchronized void load(InputStream inStream) throws IOException {
? ?load0(new LineReader(inStream));
}讀取到配置文件后,Properties對象還提供了getProperty(String key)方法來獲取指定屬性的值,如果在配置中當(dāng)前屬性不存在,則返回null值;
或者使用另一個(gè)getProperty(String key, String defaultValue)方法在獲取屬性時(shí)指定一個(gè)默認(rèn)值,如果屬性不存在則使用指定值代替null。
//獲取key對應(yīng)值,如果key不存在,則返回null
public String getProperty(String key) {
? ?Object oval = super.get(key);
? ?String sval = (oval instanceof String) ? (String)oval : null;
? ?return ((sval == null) && (defaults != null)) ? defaults.getProperty(key) : sval;
}
?
//如果不存在key,則返回defaultValue
public String getProperty(String key, String defaultValue) {
? ?String val = getProperty(key);
? ?return (val == null) ? defaultValue : val;
}2.3 輸出到流
使用Properties對象將屬性輸出到字節(jié)流中的流程可以表示為:
- 實(shí)例化一個(gè)Properties對象,
- Properties對象使用setProperty()方法設(shè)置屬性和值,
- 調(diào)用對象的store()方法將對象通過字節(jié)流保存到相應(yīng)文件。
對應(yīng)的方法定義有:
//設(shè)置屬性,本質(zhì)是Hashtable的put方法
public synchronized Object setProperty(String key, String value) {
? ?return put(key, value);
}
?
//輸出為字符流對象Writer
public void store(Writer writer, String comments)
? ? ? ?throws IOException
{
? ?store0((writer instanceof BufferedWriter)?(BufferedWriter)writer
? ? ? ? ? : new BufferedWriter(writer),
? ? ? ? ? comments,
? ? ? ? ? false);
}
?
//輸出為OutputStream流
public void store(OutputStream流 out, String comments)
? ? ? ?throws IOException
{
? ?store0(new BufferedWriter(new OutputStreamWriter(out, "8859_1")),
? ? ? ? ? comments,
? ? ? ? ? true);
}3. 讀取配置文件的幾種方式
了解Properties讀取文件的流程后,來看一下實(shí)際在項(xiàng)目中讀取自定義配置文件內(nèi)容的方式都有哪些。
1.類的getResourceAsStream方法
//類的getResourceAsStream方法,路徑加"/"時(shí)代表讀取resources目錄下內(nèi)容
InputStream inputStream = PropertiesUtils.class.getResourceAsStream方法,路徑加"/"時(shí)代表讀取resources目錄下內(nèi)容("/test.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("test.content"));2.類啟動(dòng)器的getResourceAsStream方法
//類啟動(dòng)器的getResourceAsStream方法,默認(rèn)獲取路徑為src/resources下的文件
InputStream inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream("test.properties");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("test.content"));3.PropertiesLoaderUtils工具類的loadAllProperties()方法
// PropertiesLoaderUtils類的loadAllProperties()方法,讀取resources目錄
Properties properties = PropertiesLoaderUtils.loadAllProperties("test.properties");
System.out.println(properties.getProperty("test.content"));以上就是使用Properties讀取配置文件的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Properties讀取配置文件的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JAVA實(shí)現(xiàn)PDF轉(zhuǎn)HTML文檔的示例代碼
本文是基于PDF文檔轉(zhuǎn)PNG圖片,然后進(jìn)行圖片拼接,拼接后的圖片轉(zhuǎn)為base64字符串,然后拼接html文檔寫入html文件實(shí)現(xiàn)PDF文檔轉(zhuǎn)HTML文檔,感興趣的可以了解一下2021-05-05
SpringBoot封裝自己的Starter的實(shí)現(xiàn)方法
這篇文章主要介紹了SpringBoot封裝自己的Starter的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-04-04
Mybatis Select Count(*)的返回值類型介紹
這篇文章主要介紹了Mybatis Select Count(*)的返回值類型,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
詳解Spring中實(shí)現(xiàn)接口動(dòng)態(tài)的解決方法
最近在工作遇到的一個(gè),發(fā)現(xiàn)網(wǎng)上的資料較少,所以想著總結(jié)分享下,下面這篇文章主要給大家介紹了關(guān)于Spring中實(shí)現(xiàn)接口動(dòng)態(tài)的解決方法,文中通過完整的示例代碼給大家介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07
SpringBoot訪問接口自動(dòng)跳轉(zhuǎn)login頁面的問題及解決
這篇文章主要介紹了SpringBoot訪問接口自動(dòng)跳轉(zhuǎn)login頁面的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12

