十年網(wǎng)站開(kāi)發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營(yíng)維護(hù)+專業(yè)推廣+無(wú)憂售后,網(wǎng)站問(wèn)題一站解決
這篇文章主要介紹了Spring使用PropertyPlaceholderConfigurer讀取文件的方法,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
陽(yáng)西ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!
一. 簡(jiǎn)介
大型項(xiàng)目中,我們往往會(huì)對(duì)我們的系統(tǒng)的配置信息進(jìn)行統(tǒng)一管理,一般做法是將配置信息配置與一個(gè)cfg.properties 的文件中,然后在我們系統(tǒng)初始化的時(shí)候,系統(tǒng)自動(dòng)讀取 cfg.properties 配置文件中的 key value(鍵值對(duì)),然后對(duì)我們系統(tǒng)進(jìn)行定制的初始化。
那么一般情況下,我們使用 的 java.util.Properties, 也就是 java 自帶的。往往有一個(gè)問(wèn)題是,每一次加載的時(shí)候,我們都需要手工的去讀取這個(gè)配置文件,一來(lái)編碼麻煩,二來(lái)代碼不優(yōu)雅,往往我們也會(huì)自己創(chuàng)建一個(gè)類(lèi)來(lái)專門(mén)讀取,并儲(chǔ)存這些配置信息。
對(duì)于 web 項(xiàng)目來(lái)說(shuō),可以通過(guò)相對(duì)路徑得到配置文件的路徑,而對(duì)于可執(zhí)行項(xiàng)目,在團(tuán)隊(duì)開(kāi)發(fā)中就需要根據(jù)各自的環(huán)境來(lái)指定 properties 配置文件的路徑了。對(duì)于這種情況可以將配置文件的路徑放在 java 虛擬機(jī) JVM 的自定義變量(運(yùn)行時(shí)參數(shù))中,例如:-Ddev.config=/dev.properties 尋找的是本機(jī)根目錄下
Spring中提供著一個(gè) PropertyPlaceholderConfigurer,這個(gè)類(lèi)是 BeanFactoryPostProcessor 的子類(lèi)。其主要的原理在是。Spring容器初始化的時(shí)候,會(huì)讀取 xml 或者 annotation 對(duì) Bean 進(jìn)行初始化。初始化的時(shí)候,這個(gè) PropertyPlaceholderConfigurer 會(huì)攔截 Bean 的初始化,初始化的時(shí)候會(huì)對(duì)配置的 ${pname} 進(jìn)行替換,根據(jù)我們 Properties 中配置的進(jìn)行替換。從而實(shí)現(xiàn)表達(dá)式的替換操作 。
二. XML 方式
方式1
#db.properties jdbc.driverClass==net.sourceforge.jtds.jdbc.Driver jdbc.url=jdbc:MySQL://localhost:3306/test? jdbc.username=anqi jdbc.password=123456 #db2.properties name=anqi age=23 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:spring-context.xml") public class TestPropertyPlaceHoder2 { @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; @Value("${name}") private String name; @Value("${age}") private int age; @Test public void testResource() { System.out.println("username: " + username); System.out.println("password: " + password); System.out.println("name: " + name); System.out.println("age: " + age); } } /* username: anqi password: 123456 name: anqi age: 23 */
classpath:db.properties classpath:db2.properties
方式2
注意:我們知道不論是使用 PropertyPlaceholderConfigurer 還是通過(guò) context:property-placeholder 這種方式進(jìn)行實(shí)現(xiàn),都需要記住,Spring框架不僅僅會(huì)讀取我們的配置文件中的鍵值對(duì),而且還會(huì)讀取 Jvm 初始化的一下系統(tǒng)的信息。有時(shí)候,我們需要將配置 Key 定一套命名規(guī)則 ,例如
jdbc.username
jdbc.password
同時(shí),我們也可以使用下面這種配置方式進(jìn)行配置,這里我配 NEVER 的意思是不讀取系統(tǒng)配置信息。
SYSTEM_PROPERTIES_MODE_FALLBACK:在解析一個(gè)占位符的變量的時(shí)候。假設(shè)不能獲取到該變量的值。就會(huì)拿系統(tǒng)屬性來(lái)嘗試, SYSTEM_PROPERTIES_MODE_OVERRIDE:在解析一個(gè)占位符的時(shí)候。會(huì)先用系統(tǒng)屬性來(lái)嘗試,然后才會(huì)用指定的屬性文件, SYSTEM_PROPERTIES_MODE_NEVER:從來(lái)都不會(huì)使用系統(tǒng)屬性來(lái)嘗試。
三. Java 編碼方式
采取編碼的方式顯然更加靈活,當(dāng)我們?cè)谧鲆粋€(gè)項(xiàng)目時(shí),在線下本地跑和在服務(wù)器線上跑時(shí),需要的參數(shù)肯定有諸多不同,我們可以通過(guò) xml java 編碼的方式來(lái)指定采用哪一個(gè)配置方案,同一個(gè)配置方案中也可以將線上配置文件的地址放在前面,沒(méi)有線上配置文件就采用本地配置的方式來(lái)運(yùn)行項(xiàng)目。
spring-context.xml
db.properties jdbc.driverClass==net.sourceforge.jtds.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test? jdbc.username=anqi jdbc.password=123456 pro=1 version=db1 db2.properties name=anqi age=23 pro=2 version=db2 db3.properties pro=3 dev.properties company=abc version=dev.config
classpath:db.properties classpath:db2.properties
classpath:db3.properties
讀取配置的工具類(lèi)
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.Resource;import java.io.File;import java.io.IOException;import java.util.*;public class PropertiesUtil extends PropertyPlaceholderConfigurer { private static Resource electResource; private static Properties configProperties = new Properties(); private static Properties programProperties = new Properties(); public PropertiesUtil() {} /** * 根據(jù) spring-context 配置文件中的配置,來(lái)將項(xiàng)目下對(duì)應(yīng)的 properties 文件加載到系統(tǒng)中 * 并且經(jīng)過(guò)特殊處理 db2.properties 不允許覆蓋掉 db1.properties 中相同的 key * @param locations */ public void setLocations(Resource... locations) { ListexistResourceList = new ArrayList<>(); Resource devConfig = getDevConfig(); if (devConfig != null) { existResourceList.add(devConfig); } Resource resource; for(int i = 0; i < locations.length; ++i) { resource = locations[i]; if (resource.exists()) { existResourceList.add(resource); //dev db.properties db2.properties } } Collections.reverse(existResourceList); //db2.properties db.properties dev if (!existResourceList.isEmpty()) { electResource = existResourceList.get(existResourceList.size() - 1); //dev } try { configProperties.load(electResource.getURL().openStream()); if (existResourceList != null && existResourceList.size() > 1) { for(int i = existResourceList.size() - 2; i >= 0; --i) { Properties backupConfig = new Properties(); //從后往前加載 db1 db2 backupConfig.load(((Resource)existResourceList.get(i)).getURL().openStream()); Iterator iterator = backupConfig.keySet().iterator(); //通過(guò)后面新添加的 db3.properties、db4.peoperties 進(jìn)行更新 db.properties //添加沒(méi)有的 key 不能覆蓋前面的 key while(iterator.hasNext()) { Object key = iterator.next(); if (!configProperties.containsKey(key)) { configProperties.put(key, backupConfig.get(key)); } } } } } catch (IOException e) { e.printStackTrace(); } } /** * 將 programConfig 的配置方案加載到 programeConfig 中 * (即將 db3.properties 加載到 programeConfig) * 包含運(yùn)行時(shí)方案(運(yùn)行時(shí)配置優(yōu)先級(jí)最高)會(huì)覆蓋 key 相同的 value * @param locations */ public void setProgramConfig (Resource... locations){ List existResourceList = new ArrayList<>(); Resource resource; for(int i = 0; i < locations.length; ++i) { resource = locations[i]; if (resource.exists()) { existResourceList.add(resource); } } if (!existResourceList.isEmpty()) { try { Iterator iterator = existResourceList.iterator(); while (iterator.hasNext()) { resource = iterator.next(); programProperties.load(resource.getURL().openStream()); } } catch (IOException e) { e.printStackTrace(); } } Resource devConfig = getDevConfig(); if (devConfig != null) { try { Properties devProperties = new Properties(); devProperties.load(devConfig.getURL().openStream()); Iterator iterator = devProperties.keySet().iterator(); while(iterator.hasNext()) { Object key = iterator.next(); programProperties.put(String.valueOf(key), devProperties.getProperty(String.valueOf(key), "")); } } catch (Exception e) { e.printStackTrace(); } } } /** * 在運(yùn)行期間傳入配置參數(shù)(可以將配置文件放在本機(jī)或服務(wù)器上) * @return */ private static Resource getDevConfig() { String s = System.getProperty("dev.config", ""); File devConfig = new File(s); return !s.trim().equals("") && devConfig.exists() && devConfig.isFile() ? new FileSystemResource(s) : null; } /** * 外部訪問(wèn) properties 配置文件中的某個(gè) key * @param key * @return */ public static String get(String key){ return programProperties.containsKey(key) ? programProperties.getProperty(key) : configProperties.getProperty(key); } public static void show() { System.out.println("db_1 keys: "+configProperties.keySet()); System.out.println("db_2 keys: "+programProperties.keySet()); }}
測(cè)試類(lèi)
package com.anqi.testPropertyPlaceHoder; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestPropertyPlaceHoder { public static void main(String[] args) { ApplicationContext al = new ClassPathXmlApplicationContext("classpath:spring-context.xml"); PropertiesUtil.show(); System.out.println(PropertiesUtil.get("version")); //-Ddev.config=/dev.properties 傳入運(yùn)行時(shí)參數(shù) System.out.println(PropertiesUtil.get("company")); System.out.println(PropertiesUtil.get("pro")); //db_1 keys: [name, jdbc.password, version, company, jdbc.url, pro, jdbc.driverClass, jdbc.username, age] //db_2 keys: [company, version, pro] //dev.config //abc //3 } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Spring使用PropertyPlaceholderConfigurer讀取文件的方法”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!