習慣了把集合定義在一個類的字段中,今天遇到想重用集合的定義,竟然不知道單獨的集合bean應該怎麼定義了,記之,以備後用。
對map來說,有一種比較搓的方法,就是直接用map的構造函數:
1
<bean id="symbolmap" class="java.util.hashmap">
2
<constructor-arg>
3
<map>
4
<entry>
5
<key><value><![cdata[us;djia]]></value></key>
6
<value><![cdata[us&dji]]></value>
7
</entry>
8
</map>
9
</constructor-arg>
10
</bean>
11
另一種稍微簡單的方法:
<bean id="emails" class="org.springframework.beans.factory.config.mapfactorybean">
<property name="sourcemap">
<map>
<entry key="pechorin" value="[email protected]"/>
<entry key="raskolnikov" value="[email protected]"/>
<entry key="stavrogin" value="[email protected]"/>
<entry key="porfiry" value="[email protected]"/>
</map>
</property>
對這種方法,sping還提供了listfactorybean, setfactorybean等類,這貌似是spring的一個可擴充架構,可以待以後進一步研究這個架構的實作方式。
最簡單的一種方式就是直接用spring中提供的util包:
1
<util:map id="emails">
2
<entry key="pechorin" value="[email protected]"/>
3
<entry key="raskolnikov" value="[email protected]"/>
4
<entry key="stavrogin" value="[email protected]"/>
5
<entry key="porfiry" value="[email protected]"/>
6
</util:map>
7
使用改方法時,xml檔案頭需要使用:
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd">
引用:
<a href="http://macrochen.iteye.com/blog/392616">http://macrochen.iteye.com/blog/392616</a>
<a href="http://springindepth.com/book/in-depth-ioc-collections.html">http://springindepth.com/book/in-depth-ioc-collections.html</a>