PropertyPlaceholderConfigurer的用法

工具類

package com.wanggs.pay.payutil;

/**
 * Created by Wgs on 2017/9/3.
 */

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;

/**
 * 擴展PropertyPlaceholderConfigurer,方便讀取屬性<br>
 * 使用方法:
 * <pre>
 * {@literal
 * <!-- spring的屬性加載器苦蒿,加載properties文件中的屬性 -->
 * <bean id="propertyConfigurer"
 * class="component.common.utils.PropertyUtil">
 * <property name="location">
 * <value>classpath:/config.properties</value>
 * </property>
 * </bean>
 *
 * 然后調(diào)用PropertyUtil.getProperty(key);即可讀取屬性文件中的內(nèi)容了
 *
 * }
 * </pre>
 *
 * @author tanghc
 */
public class PropertyUtil
        extends PropertyPlaceholderConfigurer {

    private static Map<String, String> propertiesData = new HashMap<String, String>();

    @Override
    protected String convertProperty(String propertyName, String propertyValue) {
        String value = super.convertProperty(propertyName, propertyValue);

        propertiesData.put(propertyName, value);

        return value;
    }

    /**
     * 獲取Properties文件中的值,如果不存在則返回defaultValue
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public static String getProperty(String key, String defaultValue) {
        String value = propertiesData.get(key);
        return value == null ? defaultValue : value;
    }

    /**
     * 獲取Properties文件中的值,不存在返回null
     *
     * @param key
     * @return
     */
    public static String getProperty(String key) {
        return getProperty(key, null);
    }

}

測試

package com.wanggs.controller;

import com.wanggs.pay.payutil.PropertyUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by Wgs on 2017/8/6.
 */
@Controller
@RequestMapping("/")
public class HelloController {
    @Value("${wx.token}")
    private String token;
    @RequestMapping("/index")
    public String index(){
        System.out.println(token);
        // PropertyPlaceholderConfigurer的用法
        String result = PropertyUtil.getProperty("wx.token");
        System.out.println(result);
        return "hello";

    }

}


springMvc 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--自動掃描@Controller-->
    <context:component-scan base-package="com.wanggs.controller">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

    <!--視圖解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--通過@Value注解讀取.properties配置內(nèi)容-->
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <!--配置文件名稱-->
                <value>classpath:config.properties</value>
            </list>
        </property>
    </bean>


    <!--PropertyPlaceholderConfigurer的配置-->
    <bean id="PropertyUtil" class="com.wanggs.pay.payutil.PropertyUtil">

        <property name="locations">

            <list>

                <value>classpath:config.properties</value>

            </list>

        </property>

    </bean>

</beans>
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市翩迈,隨后出現(xiàn)的幾起案子卦绣,更是在濱河造成了極大的恐慌妓局,老刑警劉巖冬阳,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丑掺,死亡現(xiàn)場離奇詭異猪钮,居然都是意外死亡品山,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進店門烤低,熙熙樓的掌柜王于貴愁眉苦臉地迎上來肘交,“玉大人,你說我怎么就攤上這事扑馁⊙纳耄” “怎么了?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵腻要,是天一觀的道長复罐。 經(jīng)常有香客問我,道長雄家,這世上最難降的妖魔是什么效诅? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上乱投,老公的妹妹穿的比我還像新娘咽笼。我一直安慰自己,他們只是感情好戚炫,可當我...
    茶點故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布剑刑。 她就那樣靜靜地躺著,像睡著了一般双肤。 火紅的嫁衣襯著肌膚如雪施掏。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天杨伙,我揣著相機與錄音其监,去河邊找鬼。 笑死限匣,一個胖子當著我的面吹牛抖苦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播米死,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼锌历,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了峦筒?” 一聲冷哼從身側(cè)響起究西,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎物喷,沒想到半個月后卤材,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡峦失,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年扇丛,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片尉辑。...
    茶點故事閱讀 40,928評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡帆精,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出隧魄,到底是詐尸還是另有隱情卓练,我是刑警寧澤,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布购啄,位于F島的核電站襟企,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏狮含。R本人自食惡果不足惜整吆,卻給世界環(huán)境...
    茶點故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一拱撵、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧表蝙,春花似錦拴测、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至汇跨,卻和暖如春务荆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背穷遂。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工函匕, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人蚪黑。 一個月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓盅惜,卻偏偏與公主長得像,于是被迫代替她去往敵國和親忌穿。 傳聞我的和親對象是個殘疾皇子抒寂,可洞房花燭夜當晚...
    茶點故事閱讀 45,937評論 2 361

推薦閱讀更多精彩內(nèi)容