Spring如何使用PropertyPlaceholderConfigurer讀取文件
這篇文章主要介紹了Spring如何使用PropertyPlaceholderConfigurer讀取文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
一. 簡(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)專(zhuān)門(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
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- 對(duì)于讀取一個(gè)配置文件采取的方案 --> <!--<property name="location" value="classpath:db.properties"/>--> <!--對(duì)于讀取多個(gè)配置文件采取的方案--> <property name="locations"> <list> <value>classpath:db.properties</value> <value>classpath:db2.properties</value> </list> </property> </bean> </beans>
#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 */
方式2
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" <context:property-placeholder location="classpath:db.properties,classpath:db2.properties"/> </beans>
注意:我們知道不論是使用 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)配置信息。
<context:property-placeholder location="classpath:db.properties,classpath:db2.properties" system-properties-mode="NEVER"/>
- 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í),在線(xiàn)下本地跑和在服務(wù)器線(xiàn)上跑時(shí),需要的參數(shù)肯定有諸多不同,我們可以通過(guò) xml java 編碼的方式來(lái)指定采用哪一個(gè)配置方案,同一個(gè)配置方案中也可以將線(xiàn)上配置文件的地址放在前面,沒(méi)有線(xiàn)上配置文件就采用本地配置的方式來(lái)運(yùn)行項(xiàng)目。
spring-context.xml
<bean> <!-- 配置 preoperties文件的加載類(lèi) --> <bean class="com.anqi.testPropertyPlaceHoder.PropertiesUtil"> <!-- 配置方案1 優(yōu)先級(jí)更高 配置方案1找不到 key 才會(huì)去配置方案 2 里面找--> <property name="locations"> <list> <!-- 這里支持多種尋址方式:classpath 和 file --> <!-- 推薦使用file的方式引入,這樣可以將配置和代碼分離 --> <!--<value>file:/conf/localpro.properties</value>--> <value>classpath:db.properties</value> <value>classpath:db2.properties</value> </list> </property> <!-- 配置方案2 --> <property name="programConfig"> <list> <value>classpath:db3.properties</value> </list> </property> </bean> </beans>
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
讀取配置的工具類(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) { List<Resource> existResourceList = 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<Resource> 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<Resource> 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 } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring Boot中@ConditionalOnProperty的使用方法
- Spring @value和@PropertySource注解使用方法解析
- Spring Boot自定義配置屬性源(PropertySource)
- Spring中property-placeholder的使用與解析詳解
- Spring boot中PropertySource注解的使用方法詳解
- spring boot 注入 property的三種方式(推薦)
- 詳解Spring Boot 自定義PropertySourceLoader
- spring-core組件詳解——PropertyResolver屬性解決器
- Spring框架讀取property屬性文件常用5種方法
相關(guān)文章
哲學(xué)家就餐問(wèn)題中的JAVA多線(xiàn)程學(xué)習(xí)
哲學(xué)家就餐問(wèn)題是1965年由Dijkstra提出的一種線(xiàn)程同步的問(wèn)題,下面我們就看一下JAVA多線(xiàn)程如何做2013-11-11Intellij IDEA菜單欄不見(jiàn)了(Main Menu as Separat
有人問(wèn)博主,關(guān)于Intellij IDEA菜單欄找不到了,被不小心的操作給隱藏了,怎么辦?下面給大家分享解決方案,感興趣的朋友跟隨小編一起看看吧2024-06-06elasticsearch bucket 之rare terms聚合使用詳解
這篇文章主要為大家介紹了elasticsearch bucket 之rare terms聚合使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11Java如何將ResultSet結(jié)果集遍歷到List中
這篇文章主要介紹了Java如何將ResultSet結(jié)果集遍歷到List中問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02史上最佳springboot Locale 國(guó)際化方案
今天給大家分享史上最佳springboot Locale 國(guó)際化方案,本文通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08SpringBoot2.X Kotlin系列之?dāng)?shù)據(jù)校驗(yàn)和異常處理詳解
這篇文章主要介紹了SpringBoot 2.X Kotlin系列之?dāng)?shù)據(jù)校驗(yàn)和異常處理詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-04-04Java 改造ayui表格組件實(shí)現(xiàn)多重排序
layui 的表格組件目前只支持單列排序,在實(shí)際應(yīng)用中并不能很好的支撐我們的業(yè)務(wù)需求。今天一時(shí)手癢,決定改造一番以支持多重排序。2021-04-04IDEA 重新導(dǎo)入依賴(lài)maven 命令 reimport的方法
這篇文章主要介紹了IDEA 重新導(dǎo)入依賴(lài)maven 命令 reimport的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04