Spring3: Dependency Configuration In Detail(Part II) 博客分类: SpringFramework Spring 3.2.2IoCBeans.xmlConfigurationCollection
程序员文章站
2024-03-22 16:28:22
...
Introduction:
In this article, we focus on the configuration file beans.xml for IoC purpose.
Focusing on initialize beans whose property are collection types.
Including <list/> <set/> <map/> and <props/> elements corrosponding to List, Set, Map and Properties in JAVA.
1. Four common collection types
1) <list>
<?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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <property name="scores"> <list> <value>123</value> <value>133</value> <value>132</value> </list> </property> <property name="addresses"> <list> <bean class="edu.xmu.domain.Address"> <property name="country" value="China"></property> </bean> <bean class="edu.xmu.domain.Address"> <property name="country" value="Canada"></property> </bean> <bean class="edu.xmu.domain.Address"> <property name="country" value="America"></property> </bean> <bean class="edu.xmu.domain.Address"> <property name="country" value="Austrilia"></property> </bean> </list> </property> </bean> </beans>
2) <map>
<?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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <property name="addressMap"> <map> <entry key="homeAddress" value-ref="homeAddress"> </entry> <entry key="workAddress" value-ref="workAddress"> </entry> </map> </property> <property name="scoreMap"> <map> <entry key="english" value="135"></entry> <entry key="math" value="145"></entry> <entry key="chinese" value="118"></entry> </map> </property> </bean> <bean id="homeAddress" class="edu.xmu.domain.Address"> <property name="country" value="china"> </property> </bean> <bean id="workAddress" class="edu.xmu.domain.Address"> <property name="country" value="america"> </property> </bean> </beans>
3) <set>
<?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-3.0.xsd"> <bean id="student" class="edu.xmu.domain.Student"> <property name="addressSet"> <set> <ref bean="homeAddress" /> <ref bean="workAddress" /> </set> </property> <property name="scoreSet"> <set> <value>122</value> <value>123</value> <value>231</value> </set> </property> </bean> <bean id="homeAddress" class="edu.xmu.domain.Address"> <property name="country" value="china"> </property> </bean> <bean id="workAddress" class="edu.xmu.domain.Address"> <property name="country" value="america"> </property> </bean> </beans>
4) <props>
Just like previous introduction. Not usually used.
2. Inner beans introduction
<bean id="student" class="edu.xmu.domain.Student"> <!-- instead of using a reference to a target bean, simply define the target bean inline --> <property name="workAddress"> <!-- This is the inner bean --> <bean class="edu.xmu.domain.Address"> <property name="country" value="China"/> <property name="province" value="Shanghai"/> </bean> </property> </bean>
Comments:
1) A <bean/>
element inside the <property/>
or <constructor-arg/>
elements defines a so-called inner beans.
2) Inner beans are always anonymous and they are always scoped as prototypes.