Spring3: Dependency Configuration In Detail(Part II)

 

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值