习惯了把集合定义在一个类的字段中,今天遇到想重用集合的定义,竟然不知道单独的集合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>