Spring核心技术----IoC的Bean基本管理4
对于像? 数组,? java.util.list,??? java.util.Set,????? java.util.map等集合对象,在注入前若必须填充一些对象至集合中,然后再将集合对象注入至所需的Bean时,也可以交由Spring的IoC容器来自动维护或生成集合对象,并完成依赖注入。
实例:
Bean定义文件
----------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<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.0.xsd">
?
?<bean id="some1" class="only.SomeBean">
?
? <!--数组的配置-->
??<property name="someStrArray">
???<list>
????<value>Hello</value>
????<value>Welcome</value>
???</list>
??</property>
??
? <!--list的配置-->
??<property name="someObjArray">
???<list>
????<ref bean="some1"/>
????<ref bean="some2"/>
???</list>
??</property>
??
?<!--list的配置-->
??<property name="someList">
???<list>
????<value>ListTest</value>
????<ref bean="some1"/>
????<ref bean="some2"/>
???</list>
??</property>
??
?<!--map的配置-->
??<property name="someMap">
???<map>
????<entry key="MapTest">
?????<value>Hello!Justin!</value>
????</entry>
????<entry key="someKey1">
?????<ref bean="some1"/>
????</entry>
???</map>
??</property>
?---------------------------------
?<!--set的配置-->
...........
????<set>
????<value>a set element</value>
????<ref bean="some1"/>
????<ref bean="some2"/>
???</set>
...........
----------------------------------
----------------------------------
?<!--Properties的配置-->
...........
???<props>
?????? <prop key="someprokey1">
????????????????? someProValue1
???????</prop>
?????? <prop key="someproke2">
????????????????? someProValue2
???????</prop>
?? </props>
..........
-----------------------------------
?</bean>
</beans>