介紹
- 背景:在java開發中,往往需要自己定義配置文件、解析方式
- java類中,進行配置文件加載的類是:Properties
- Properties的是一個持久的屬性值,可保存在流中,或者從流中加載。配置裏的每個鍵值對,都是String類型
- Properties是一個線程安全的類,多個線程可共享同一個Properties對象:因爲load和set操作,使用了syncronized修飾
加載方式:
- 方式1:文件系統加載
- 方式2:類加載器加載
- 方式3【第三方庫,這裏不做介紹】:使用apache的配置包:org.apache.commons.configuration
- 方式4:使用註解方式,待完成!!!
- 示例:
- 創建配置文件1:ref2.properties,放入到工程的resources目錄下:
cat=reflect2.Cat bird=reflect2.Bird
- 創建配置文件2:ref2.yaml,放入到工程的resources目錄下:
cat: reflect2.Cat bird: reflect2.Bird
- 創建測試代碼:
class Main1 { public static void main(String[] args) throws IOException { // 方式1:文件系統加載: // 問題:文件系統加載的時候,不容易找到配置文件的路徑 System.out.println("################### 方式1:文件系統加載: ###################"); FileInputStream fis1 = new FileInputStream( "/Users/zhaoyue/codes/springAopMaven/src/main/resources/ref2.properties"); Properties p1 = new Properties(); p1.load(fis1); System.out.println("獲取cat的value:" + p1.getProperty("cat")); FileInputStream fis2 = new FileInputStream( "/Users/zhaoyue/codes/springAopMaven/src/main/resources/ref2.yaml"); Properties p2 = new Properties(); p2.load(fis2); System.out.println("獲取bird的value:" + p2.getProperty("bird")); // 方式2:通過類加載器 // 一般情況下,配置文件都放置在:resources目錄下 System.out.println("################### 方式2:通過類加載器: ###################"); InputStream resourceAsStream = Main1.class.getClassLoader().getResourceAsStream("ref2.properties"); Properties p3 = new Properties(); p3.load(resourceAsStream); System.out.println("獲取bird的value:" + p3.getProperty("bird")); } }
- 結果說明:
不論是properties還是yaml文件,Properties都可進行加載、解析
通常情況下,優先推薦使用類加載器方式加載配置文件
Properties類:
-
屬性:
protected Properties defaults; -
構造方法:
public Properties()
Public Properties(Properties defaults) -
加載配置:
public synchronized void load(Reader reader) throws IOException // 按簡單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)。
public synchronized void load(InputStream inStream) throws IOException // 從輸入流中讀取屬性列表(鍵和元素對)。 -
獲取配置:
public String getProperty(String key) // 用指定的鍵在此屬性列表中搜索屬性
public String getProperty(String key, String defaultValue) // 用指定的鍵在屬性列表中搜索屬性,不存在時設置默認值
public Enumeration<?> propertyNames() // 返回屬性列表中所有鍵的枚舉,如果在主屬性列表中未找到同名的鍵,則包括默認屬性列表中不同的鍵。
public Set stringPropertyNames() // 返回此屬性列表中的鍵集,其中該鍵及其對應值是字符串,如果在主屬性列表中未找到同名的鍵,則還包括默認屬性列表中不同的鍵 -
修改配置:線程安全
public synchronized Object setProperty(String key, String value) // 調用 Hashtable 的方法 put。 -
保存配置:
public void save(OutputStream out, String comments) // 已廢棄,不用care
public void store(Writer writer, String comments) throws IOException // 以適合使用 load(Reader) 方法的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出字符。
public void store(OutputStream out, String comments) throws IOException // 以適合使用 load(InputStream) 方法加載到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
public synchronized void loadFromXML(InputStream in) throws IOException, InvalidPropertiesFormatException // 將指定輸入流中由 XML 文檔所表示的所有屬性加載到此屬性表中。
public void storeToXML(OutputStream os, String comment) throws IOException // 發出一個表示此表中包含的所有屬性的 XML 文檔。
public void storeToXML(OutputStream os, String comment, String encoding) throws IOException // 使用指定的編碼發出一個表示此表中包含的所有屬性的 XML 文檔。
public void list(PrintStream out) // 將屬性列表輸出到指定的輸出流。
public void list(PrintWriter out) // 將屬性列表輸出到指定的輸出流。 -
匿名內部類:
class LineReader:用於讀取配置文件中的每一行
private static class XmlSupport : Xml解析的支持
- 簡單的使用(保存配置相關這裏不做介紹)
// 方式2:通過類加載器
// 一般情況下,配置文件都放置在:resources目錄下
System.out.println("################### 方式2:通過類加載器: ###################");
InputStream resourceAsStream = Main1.class.getClassLoader().getResourceAsStream("ref2.properties");
Properties p3 = new Properties();
p3.load(resourceAsStream);
System.out.println("獲取bird的value:" + p3.getProperty("bird"));
System.out.println("################### 測試get方法 ###################");
System.out.println("獲取birdsss的value(默認爲null):" + p3.getProperty("birdsss"));
System.out.println("獲取birdsss的value(可傳入默認值):" + p3.getProperty("birdsss", "Nothing"));
System.out.println("################### 打印所有的屬性值 ###################");
System.out.println("方式1:通過枚舉值獲取");
Enumeration<?> enumeration = p3.propertyNames(); // 可火速所有的屬性值
while (enumeration.hasMoreElements()) {
System.out.println(enumeration.nextElement());
}
System.out.println("方式2:set<String>集合");
System.out.println(p3.stringPropertyNames());
System.out.println("################### 修改屬性值 ###################");
p3.setProperty("cat", "NoCat");
System.out.println("修改後的cat爲:" + p3.getProperty("cat"));
- 輸出:
################### 方式2:通過類加載器: ###################
獲取bird的value:reflect2.Bird
################### 測試get方法 ###################
獲取birdsss的value(默認爲null):null
獲取birdsss的value(可傳入默認值):Nothing
################### 打印所有的屬性值 ###################
方式1:通過枚舉值獲取
cat
bird
方式2:set<String>集合
[cat, bird]
################### 修改屬性值 ###################
修改後的cat爲:NoCat
- 除了自身去load之外,還可以調用System.getProperties()
System.out.println("################### 獲取系統的屬性值 ###################");
Properties p4 = System.getProperties();
System.out.println(p4.stringPropertyNames());
輸出:可以看到,打印了java運行時的所有入參
################### 獲取系統的屬性值 ###################
[java.runtime.name, sun.boot.library.path, java.vm.version, gopherProxySet, java.vm.vendor, java.vendor.url, path.separator, java.vm.name, file.encoding.pkg, user.country, sun.java.launcher, sun.os.patch.level, java.vm.specification.name, user.dir, java.runtime.version, java.awt.graphicsenv, java.endorsed.dirs, os.arch, java.io.tmpdir, line.separator, java.vm.specification.vendor, os.name, sun.jnu.encoding, java.library.path, java.specification.name, java.class.version, sun.management.compiler, os.version, user.home, user.timezone, java.awt.printerjob, file.encoding, java.specification.version, user.name, java.class.path, java.vm.specification.version, sun.arch.data.model, java.home, sun.java.command, java.specification.vendor, user.language, awt.toolkit, java.vm.info, java.version, java.ext.dirs, sun.boot.class.path, java.vendor, file.separator, java.vendor.url.bug, sun.cpu.endian, sun.io.unicode.encoding, sun.cpu.isalist]
其它
- 參考1:https://www.jianshu.com/p/efdd1a526939
- 參考2:https://tool.oschina.net/apidocs/apidoc?api=jdk-zh
- 參考3:https://www.iteye.com/blog/liuzidong-831440