Spring MapFactoryBean example

The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.
Here’s a MapFactoryBean example, it will instantiate a HashMap at runtime,, and inject it into a bean property.

package com.mkyong.common;
import java.util.Map;
public class Customer { 
    private Map maps; 
    //...
}

Spring’s bean configuration file.

<?xml version="1.0"?>
<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-2.5.xsd">
    <bean id="CustomerBean" class="com.mkyong.common.Customer">
        <property name="maps">
            <bean class="org.springframework.beans.factory.config.MapFactoryBean">
                <property name="targetMapClass">
                    <value>java.util.HashMap</value>
                </property>
                <property name="sourceMap">
                    <map>
                        <entry key="Key1" value="1" />
                        <entry key="Key2" value="2" />
                        <entry key="Key3" value="3" />
                    </map>
                </property>
            </bean>
        </property>
    </bean>
</beans>

Alternatively, you also can use util schema and <util:map> to achieve the same thing.

<?xml version="1.0"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
    <bean id="CustomerBean" class="com.mkyong.common.Customer">
        <property name="maps">
            <util:map map-class="java.util.HashMap">
                <entry key="Key1" value="1" />
                <entry key="Key2" value="2" />
                <entry key="Key3" value="3" />
            </util:map>
        </property>
    </bean>
</beans>

Remember to include the util schema, else you will hit the following error

Caused by: org.xml.sax.SAXParseException: The prefix "util" for element "util:map" is not bound.

Run it…

package com.mkyong.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App { 
    public static void main( String[] args ) { 
        ApplicationContext context = new ClassPathXmlApplicationContext("SpringBeans.xml"); 
        Customer cust = (Customer)context.getBean("CustomerBean"); 
        System.out.println(cust); 
    }
}

Ouput

Customer [maps={Key2=2, Key1=1, Key3=3}] Type=[class java.util.HashMap]

You have instantiated a HashMap and injected it into Customer’s map property at runtime.

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末恕刘,一起剝皮案震驚了整個濱河市照雁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌揽趾,老刑警劉巖小作,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件亭姥,死亡現(xiàn)場離奇詭異,居然都是意外死亡顾稀,警方通過查閱死者的電腦和手機达罗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來静秆,“玉大人粮揉,你說我怎么就攤上這事「П剩” “怎么了扶认?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長塔沃。 經(jīng)常有香客問我蝠引,道長,這世上最難降的妖魔是什么蛀柴? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任螃概,我火速辦了婚禮,結(jié)果婚禮上鸽疾,老公的妹妹穿的比我還像新娘吊洼。我一直安慰自己,他們只是感情好制肮,可當我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布冒窍。 她就那樣靜靜地躺著递沪,像睡著了一般。 火紅的嫁衣襯著肌膚如雪综液。 梳的紋絲不亂的頭發(fā)上款慨,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天,我揣著相機與錄音谬莹,去河邊找鬼檩奠。 笑死,一個胖子當著我的面吹牛附帽,可吹牛的內(nèi)容都是我干的埠戳。 我是一名探鬼主播,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼蕉扮,長吁一口氣:“原來是場噩夢啊……” “哼整胃!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起喳钟,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤屁使,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后荚藻,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屋灌,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年应狱,在試婚紗的時候發(fā)現(xiàn)自己被綠了共郭。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡疾呻,死狀恐怖除嘹,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情岸蜗,我是刑警寧澤尉咕,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站璃岳,受9級特大地震影響年缎,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜铃慷,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一单芜、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧犁柜,春花似錦洲鸠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽绢淀。三九已至,卻和暖如春瘾腰,著一層夾襖步出監(jiān)牢的瞬間皆的,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工居灯, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留祭务,地道東北人。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓怪嫌,卻偏偏與公主長得像,于是被迫代替她去往敵國和親柳沙。 傳聞我的和親對象是個殘疾皇子岩灭,可洞房花燭夜當晚...
    茶點故事閱讀 45,047評論 2 355

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

  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,166評論 2 7
  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn)赂鲤,斷路器噪径,智...
    卡卡羅2017閱讀 134,662評論 18 139
  • 黑點閱讀 271評論 0 0
  • 照片是2015年攝于羅布人村寨泡孩。當時拍完就放在了電腦里车摄,僅僅是放在電腦里了,文字寫于今晚仑鸥! 很多沒有來過沙漠的人吮播,...
    至簡從心閱讀 1,024評論 33 18
  • 但是需要讀者具備最基本的"Shell知識"和了解"公鑰加密"的概念。如果你對它們不熟悉眼俊,我推薦先閱讀《UNIX /...
    清水蘆葦閱讀 214評論 0 0