您好,登錄后才能下訂單哦!
Java作為高級(jí)程序語言中的老將,自然擁有很多技術(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等等。后面我們會(huì)選擇十來個(gè)有用的Utils,給大家展示一下Spring中的工具類的有用和常用方法。
org.springframework.core.io.support.PropertiesLoaderUtils
我們今天第一個(gè)介紹的是PropertiesLoaderUtils,這個(gè)工具類主要是針對(duì)Properties文件的加載操作,在Spring對(duì).properties文件和.factories文件的操作都有使用到。
先來簡(jiǎn)單看看這個(gè)類提供的有用方法:
方法不是很多,而且共性較大,我們就從最簡(jiǎn)單的Properties loadProperties(Resource resource)開始。
loadProperties
測(cè)試方法很簡(jiǎn)單,我們首先準(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"));
}
測(cè)試完成。沒有太大難度; 但是,其實(shí)Properties對(duì)象不僅僅支持.properties文件,還支持XML格式的資源配置文件。先來看看Properties對(duì)象對(duì)XML類型資源文件的DTD定義(http://java.sun.com/dtd/properties.dtd):
<!--
???Copyright 2006 Sun Microsystems, Inc.??All rights reserved.
??-->
<!-- DTD for properties -->
<!ELEMENT properties ( comment?, entry* ) >
<!ATTLIST properties version CDATA #FIXED "1.0">
<!ELEMENT comment (#PCDATA) >
<!ELEMENT entry (#PCDATA) >
<!ATTLIST entry key CDATA #REQUIRED>
那么我們就可以有以下測(cè)試。創(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>
完成測(cè)試代碼:
@Test
public void testLoadPropertiesResourceXml() throws Exception {
????Properties ret = PropertiesLoaderUtils
????????????.loadProperties(new ClassPathResource("test.xml"));
????assertEquals("value", ret.getProperty("key"));
????assertEquals("中文", ret.getProperty("key2"));
}
測(cè)試通過。當(dāng)然,我們是非常不建議使用XML的方式來做配置的。
接下來使用EncodeResource來測(cè)試,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è)測(cè)試。把上一個(gè)例子中的test.properties文件保留,放到src/main/resources中;然后在src/test/resources目錄下再創(chuàng)建一個(gè)test.properties文件,內(nèi)容為:
key3=value
測(cè)試代碼:
@Test
public void testLoadAllPropertiesString() throws Exception {
????Properties ret = PropertiesLoaderUtils
????????????.loadAllProperties("test.properties");
????assertEquals("value", ret.getProperty("key"));
????assertEquals("value", ret.getProperty("key3"));
}
執(zhí)行測(cè)試通過;可以看到,main下的test.properties和test下的test.properties都被識(shí)別并執(zhí)行了合并;那如果在test/resources中的test.properties中增加
key=update
再次執(zhí)行測(cè)試,仍然通過,說明在多個(gè)配置文件中如果有重復(fù)key,最近的classpath為準(zhǔn)(比如當(dāng)前應(yīng)用的properties內(nèi)容會(huì)優(yōu)先于jar包中的properties)
但是,如果這種情況下,使用loadProperties方法,那么只會(huì)加載到test/resources中的test.properties;這個(gè)結(jié)果可以對(duì)loadAllProperties有更深刻理解。
fillProperties
fillProperties方法其實(shí)是loadProperties方法的真正調(diào)用方法。先來看看測(cè)試代碼:
@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"));
}
使用非常簡(jiǎn)單。
我們來看一下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í)也很簡(jiǎn)單:
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對(duì)象的load和loadFromXML方法完成的。
小結(jié)
Properties對(duì)象的load和loadFromXML方法是核心,重要知識(shí)要?jiǎng)澲攸c(diǎn)。后續(xù)會(huì)推出更多spring utils,歡迎持續(xù)關(guān)注。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。