Java作為高級程序語言中的老將改橘,自然擁有很多技術(shù)工具,其中Spring框架為人所熟知自脯,里面也有很多utils提供給開發(fā)者贞谓,隨我一起來看看吧!
前言
Spring的工具類都是以Utils結(jié)尾叛溢,所以要查看這些工具類塑悼,只需要在API文檔中查詢所有*Utils即可,可以看到有多達(dá)幾十個(gè)楷掉。其中有我們非常熟悉的org.springframework.util.StringUtils厢蒜,有用到過的org.springframework.aop.support.AopUtils,還有可能沒有聽過的org.springframework.core.annotation.AnnotatedElementUtils等等烹植。后面我們會選擇十來個(gè)有用的Utils斑鸦,給大家展示一下Spring中的工具類的有用和常用方法。
org.springframework.core.io.support.PropertiesLoaderUtils
我們今天第一個(gè)介紹的是PropertiesLoaderUtils草雕,這個(gè)工具類主要是針對Properties文件的加載操作巷屿,在Spring對.properties文件和.factories文件的操作都有使用到。
先來簡單看看這個(gè)類提供的有用方法:
- Properties loadProperties(Resource resource) throws IOException:從一個(gè)資源文件加載Properties墩虹;
- Properties loadProperties(EncodedResource resource) throws IOException:加載資源文件嘱巾,傳入的是提供了編碼的資源類(EncodedResource)憨琳;和上面方法基本一致;
- void fillProperties(Properties props, Resource resource) throws IOException:從一個(gè)資源類中加載資源浓冒,并填充到指定的Properties對象中栽渴;
- void fillProperties(Properties props, EncodedResource resource)
throws IOException:從一個(gè)編碼資源類中加載資源,并填充到指定的Properties對象中稳懒;和上面方法基本一致闲擦; - Properties loadAllProperties(String resourceName) throws IOException:根據(jù)資源文件名稱,加載并合并classpath中的所有資源文件场梆;
- Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException:從指定的ClassLoader中墅冷,根據(jù)資源文件名稱,加載并合并classpath中的所有資源文件或油;
方法不是很多寞忿,而且共性較大,我們就從最簡單的Properties loadProperties(Resource resource)開始顶岸。
loadProperties
測試方法很簡單腔彰,我們首先準(zhǔn)備一個(gè)test.properties文件,放到resources下面:
key=value
key2=\u4E2D\u6587
完成代碼:
@Test
public void testLoadPropertiesResource() throws Exception {
Properties ret = PropertiesLoaderUtils
.loadProperties(new ClassPathResource("test.properties"));
assertEquals("value", ret.getProperty("key"));
assertEquals("中文", ret.getProperty("key2"));
}
測試完成辖佣。沒有太大難度霹抛; 但是,其實(shí)Properties對象不僅僅支持.properties文件卷谈,還支持XML格式的資源配置文件杯拐。先來看看Properties對象對XML類型資源文件的DTD定義(http://java.sun.com/dtd/properties.dtd):
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
那么我們就可以有以下測試。創(chuàng)建一個(gè)text.xml文件在classpath下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>一些自定義說明</comment>
<entry key="key">value</entry>
<entry key="key2">中文</entry>
</properties>
完成測試代碼:
@Test
public void testLoadPropertiesResourceXml() throws Exception {
Properties ret = PropertiesLoaderUtils
.loadProperties(new ClassPathResource("test.xml"));
assertEquals("value", ret.getProperty("key"));
assertEquals("中文", ret.getProperty("key2"));
}
測試通過世蔗。當(dāng)然端逼,我們是非常不建議使用XML的方式來做配置的。
接下來使用EncodeResource來測試污淋,EncodeResource在Resource上增加了字符編碼設(shè)置顶滩。同樣使用之前的test.properties:
@Test
public void testLoadPropertiesEncodedResource() throws Exception {
Properties ret = PropertiesLoaderUtils.loadProperties(
new EncodedResource(new ClassPathResource("test.properties"),
"UTF-8"));
assertEquals("value", ret.getProperty("key"));
assertEquals("中文", ret.getProperty("key2"));
}
可以看到,只是在之前的ClassPathResource基礎(chǔ)之上包裝了UTF-8字符編碼的EncodeResource寸爆;
loadAllProperties
loadProperties方法诲祸,從當(dāng)前classpath下加載properties文件,如果使用loadAllProperties而昨,可以從當(dāng)前classpath下加載所有的相同名稱的properties文件救氯,并執(zhí)行合并。
來完成一個(gè)測試歌憨。把上一個(gè)例子中的test.properties文件保留着憨,放到src/main/resources中;然后在src/test/resources目錄下再創(chuàng)建一個(gè)test.properties文件务嫡,內(nèi)容為:
key3=value
測試代碼:
@Test
public void testLoadAllPropertiesString() throws Exception {
Properties ret = PropertiesLoaderUtils
.loadAllProperties("test.properties");
assertEquals("value", ret.getProperty("key"));
assertEquals("value", ret.getProperty("key3"));
}
執(zhí)行測試通過甲抖;可以看到漆改,main下的test.properties和test下的test.properties都被識別并執(zhí)行了合并;那如果在test/resources中的test.properties中增加
key=update
再次執(zhí)行測試准谚,仍然通過挫剑,說明在多個(gè)配置文件中如果有重復(fù)key,最近的classpath為準(zhǔn)(比如當(dāng)前應(yīng)用的properties內(nèi)容會優(yōu)先于jar包中的properties)
但是柱衔,如果這種情況下樊破,使用loadProperties方法,那么只會加載到test/resources中的test.properties唆铐;這個(gè)結(jié)果可以對loadAllProperties有更深刻理解哲戚。
fillProperties
fillProperties方法其實(shí)是loadProperties方法的真正調(diào)用方法。先來看看測試代碼:
@Test
public void testFillPropertiesPropertiesResource() throws Exception {
Resource res = new ClassPathResource("test.properties");
Properties ret = new Properties();
PropertiesLoaderUtils.fillProperties(ret, res);
assertEquals("value", ret.getProperty("key"));
}
使用非常簡單艾岂。
我們來看一下loadProperties方法的源代碼:
/**
Load properties from the given EncodedResource,
potentially defining a specific encoding for the properties file.
@see #fillProperties(java.util.Properties, EncodedResource)
*/
public static Properties loadProperties(EncodedResource resource) throws IOException {
Properties props = new Properties();
fillProperties(props, resource);
return props;
}
可以看到顺少,其實(shí)調(diào)用的就是fileProperties方法,而這個(gè)方法的實(shí)現(xiàn)其實(shí)也很簡單:
public static void fillProperties(Properties props, EncodedResource resource)
throws IOException {
fillProperties(props, resource, new DefaultPropertiesPersister());
}
代理給了fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)方法王浴;只是最后一個(gè)參數(shù)是一個(gè)PropertiesPersister脆炎,抽取了一個(gè)統(tǒng)一的從XML或者properties資源加載和裝載接口;來看看具體實(shí)現(xiàn)(我只保留了最基本的關(guān)鍵代碼):
static void fillProperties(Properties props, EncodedResource resource, PropertiesPersister persister)
throws IOException {
InputStream stream = null;
Reader reader = null;
try {
String filename = resource.getResource().getFilename();
//如果是XML文件氓辣,
if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) {
stream = resource.getInputStream();
persister.loadFromXml(props, stream);
}else {
stream = resource.getInputStream();
persister.load(props, stream);
}
}finally {
//close方法
}
}
可以看到秒裕,其實(shí)就是調(diào)用了PropertiesPersister的loadFromXml和load方法來分別加載XML或properties;
如果再往下看筛婉,有興趣的童鞋可以看看DefaultPropertiesPersister的相關(guān)的這兩個(gè)方法簇爆,其實(shí)核心還是使用Properties對象的load和loadFromXML方法完成的癞松。
小結(jié)
Properties對象的load和loadFromXML方法是核心爽撒,重要知識要?jiǎng)澲攸c(diǎn)。后續(xù)會推出更多spring utils响蓉,歡迎持續(xù)關(guān)注硕勿。