Spring 3.X 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"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:util="http://www.springframework.org/schema/util"
	  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
	  http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

	  <!-- 整合多个配置文件 -->
	  <import resource="classpath:com/baobaotao/beanfactory/beans.xml"/>
	  <bean id="car1" class="com.baobaotao.beanfactory.Car" init-method="myInit" destroy-method="myDestroy">
	  	<property name="brand" value="redCarABC5" />
	  	<property name="color" value="red"/>
	  	<property name="maxSpeed" value="2000" />  
	  </bean>
	  
	  <!-- 使用 P 的命名空间的配置 -->
	  <bean id="car5" class="com.baobaotao.beanfactory.Car" init-method="myInit" destroy-method="myDestroy"
	  	p:brand="redCarQEW5"
	  	p:maxSpeed="200"
	  	p:price="20000"
	  	/>
	  <bean id="boss6" class="com.baobaotao.beanfactory.Boss"
	  	p:car-ref="car5"/>
	  	
	  <bean id="car2" class="com.baobaotao.beanfactory.Car">
	  <!-- 简化配置 -->
	 	<!-- <property name="maxSpeed" value="200" /> -->
	  	<property name="maxSpeed">
	  		<value>200</value>
	  	</property>
	  	<property name="brand">
	  		<value>red CA72</value>
	  	</property>
	  	<property name="price">
	  		<value>20000</value>
	  	</property>
	  </bean>
	  
	  <bean id="car3" class="com.baobaotao.beanfactory.Car">
	 	<!-- 简化配置 -->
	  	<!--<constructor-arg type="java.lang.String" value="Red CA72"/>  -->
	  	<constructor-arg type="java.lang.String">
	  		<value>Red CA72</value>
	  	</constructor-arg>
	  	<constructor-arg type="java.lang.int">
	  		<value>2000</value>
	  	</constructor-arg>
	  </bean>
	  
	  <bean id="car4" class="com.baobaotao.beanfactory.Car">
	  	<constructor-arg index="0" value="RedCA72"/>
	  	<constructor-arg index="1" value="black"/>
	  	<constructor-arg index="2" value="200"/>
	  	<!-- 构造函数参数的简化 -->
	  	<!--<constructor-arg>
	  		<ref bean="car"/>
	  	</constructor-arg>
	  	<constructor-arg ref="car"/>
	  	  -->
	  	  
	  </bean>
	  
	  <!-- 引用其他 Bean -->
	  <bean id="boss" class="com.babaotao.beanfactory.Boss">
	  <!-- 简化配置 -->
	  <!--<property name="car" ref="car1" />  -->
	  	<property name="car">
	  		<ref bean="car1"/>
	  	</property>
	  </bean>
	  <!-- 内部 Bean -->
	  <bean id="boss1" class="com.baobaotao.beanfactory.Boss">
	  	<property name="car">
	  		<bean class="com.baobaotao.beanfactory.Car">
	  			<property name="maxSpeed">
	  				<value>200</value>
	  			</property>
	  			<property name="brand">
	  				<value>red CA72</value>
	  			</property>
	  			<property name="price">
	  				<value>20000</value>
	  			</property>
	  		</bean>
	  	</property>
	  </bean>
	 <!-- 集合类型属性 List-->
	 <bean id="boss2" class="com.baobaotao.beanfactory.Boss">
	 	<property name="favourites">
	 		<list>
	 			<value>Reading</value>
	 			<value>Eatting</value>
	 			<value>Golf</value>
	 		</list>
	 	</property>
	 </bean> 
	 <!-- Map 类型的属性 -->
	 <bean id="boss3" class="com.baobaotao.beanfactory.Boss">
	 	<property name="jobs">
	 		<map>
	 		<!-- 简化配置 -->
	 		<!--<entry key="AM" value="Meeting Guset"/> -->
	 			<entry>
		 			<key><value>AM</value></key>
		 			<value>Meeting Guset</value>
	 			</entry>
	 			<entry>
	 				<key><value>PM</value></key>
	 				<value>Internal Meeting</value>
	 			</entry>
	 		</map>
	 	</property>
	 </bean>
	 <!--  Properties 类型-->
	 <bean id="boos4" class="com.baobaotao.beanfactory.Boss">
	 	<property name="mails">
	 		<props>
	 			<prop key="jobMail">[email protected]</prop>
	 			<prop key="lifeMail">[email protected]</prop>
	 		</props>
	 	</property>
	 </bean>
	 <!-- JDK 5.0 提供的强类型集合的性功能 -->
	 <bean id="boos5" class="com.baobaotao.beanfactory.Boss">
	 	<property name="jobTime">
	 		<map>
	 			<entry>
	 				<key><value>Meeting Guest</value></key>
	 				<value>124</value>
	 			</entry>
	 		</map>
	 	</property>
	 </bean>
	 <!-- 通过 util 命名空间配置集合类型 Bean -->
	 <util:list id="favoriteList1" list-class="java.util.LinkedList">
	 	<value>Reading</value>
	 	<value>Car Game</value>
	 	<value>Golf</value>
	 </util:list>
	 <util:set id="favoriteSet" set-class="java.util.HashSet">
	 	<value>Reading</value>
	 	<value>Car Game</value>
	 	<value>Golf</value>
	 </util:set>
	 <util:map id="favoriteMap" >
	 	<entry key="AM" value="Meeting Guset"/>
	 	<entry key="PM" value="Internal Meeting"/>
	 </util:map>
	 <!-- <ref> 的简化形式对应于<ref bean="XXX">,而<ref local="XXX"> 和 <ref parent="XXX">没有对应的简化形式  -->
</beans>












你可能感兴趣的:(Spring 3)