欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Spring工具類PropertiesLoaderUtils的介紹

這篇文章給大家分享的是Spring工具類PropertiesLoaderUtils的介紹。小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí)。如下資料是關(guān)于PropertiesLoaderUtils的內(nèi)容。

成都創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),三元企業(yè)網(wǎng)站建設(shè),三元品牌網(wǎng)站建設(shè),網(wǎng)站定制,三元網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,三元網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

前言

Spring的工具類都是以Utils結(jié)尾,所以要查看這些工具類,只需要在API文檔中查詢所有*Utils即可,可以看到有多達(dá)幾十個。其中有我們非常熟悉的org.springframework.util.StringUtils,有用到過的org.springframework.aop.support.AopUtils,還有可能沒有聽過的org.springframework.core.annotation.AnnotatedElementUtils等等。后面我們會選擇十來個有用的Utils,給大家展示一下Spring中的工具類的有用和常用方法。

org.springframework.core.io.support.PropertiesLoaderUtils

我們今天第一個介紹的是PropertiesLoaderUtils,這個工具類主要是針對Properties文件的加載操作,在Spring對.properties文件和.factories文件的操作都有使用到。

先來簡單看看這個類提供的有用方法:

  • Properties loadProperties(Resource resource) throws IOException:從一個資源文件加載Properties;
  • Properties loadProperties(EncodedResource resource) throws IOException:加載資源文件,傳入的是提供了編碼的資源類(EncodedResource);和上面方法基本一致;
  • void fillProperties(Properties props, Resource resource) throws IOException:從一個資源類中加載資源,并填充到指定的Properties對象中;
  • void fillProperties(Properties props, EncodedResource resource)
    throws IOException:從一個編碼資源類中加載資源,并填充到指定的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)備一個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):

<!--

   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>

那么我們就可以有以下測試。創(chuàng)建一個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í)行合并。

來完成一個測試。把上一個例子中的test.properties文件保留,放到src/main/resources中;然后在src/test/resources目錄下再創(chuàng)建一個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í)行測試,仍然通過,說明在多個配置文件中如果有重復(fù)key,最近的classpath為準(zhǔn)(比如當(dāng)前應(yīng)用的properties內(nèi)容會優(yōu)先于jar包中的properties)

但是,如果這種情況下,使用loadProperties方法,那么只會加載到test/resources中的test.properties;這個結(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方法,而這個方法的實(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)方法;只是最后一個參數(shù)是一個PropertiesPersister,抽取了一個統(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;

看完上述內(nèi)容,你們對Spring的工具類有進(jìn)一步的了解嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀。

當(dāng)前標(biāo)題:Spring工具類PropertiesLoaderUtils的介紹
標(biāo)題路徑:http://chinadenli.net/article22/gieecc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)網(wǎng)站維護(hù)面包屑導(dǎo)航企業(yè)網(wǎng)站制作網(wǎng)站建設(shè)服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)