日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring MapFactoryBean例子

發布時間:2024/9/20 javascript 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MapFactoryBean例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
MapFactoryBean類為開發者提供了一種在Spring的bean配置文件中創建一個具體的Map集合類(HashMap和TreeMap)。 這里有一個MapFactoryBean。例如,在運行時它將實例化一個HashMap,并注入到一個bean屬性。 package com.yiibai.common;import java.util.Map;public class Customer {private Map maps;//... } Spring 的 bean 配置文件。 <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.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="one" /><entry key="Key2" value="two" /><entry key="Key3" value="three" /></map></property></bean></property></bean></beans> 另外,還可以使用?util 的模式和<util:map>來做到同樣的事情。 <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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-2.5.xsd"><bean id="CustomerBean" class="com.yiibai.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> 請記住包函util模式,否則會出現下面的錯誤 Caused by: org.xml.sax.SAXParseException: The prefix "util" for element "util:map" is not bound.

執行結果...

package com.yiibai.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);} }

輸出結果:

Customer?[maps={Key2=two,?Key1=one,?Key3=three}] 在運行時實例化一個HashMap和注入到客戶的映射(Map)屬性。 下載代碼 –?http://pan.baidu.com/s/1kTXsDoj http://www.yiibai.com/spring/spring-mapfactorybean-example.html

總結

以上是生活随笔為你收集整理的Spring MapFactoryBean例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。