1. 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- beans节点中的红色部分是我加的,否则报出“The matching wildcard is strict, but no declaration can be found for element 'util:list"错误-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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-3.1.xsd">
<bean id="car" class="com.hxzy.model.Car">
<constructor-arg type="java.lang.String">
<value>宝马</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>天津</value>
</constructor-arg>
<constructor-arg type="int">
<value>200</value>
</constructor-arg>
</bean>
<bean id="car2" class="com.hxzy.model.Car">
<constructor-arg type="java.lang.String">
<value>奔驰</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>北京</value>
</constructor-arg>
<constructor-arg type="int">
<value>220</value>
</constructor-arg>
</bean>
<bean id="person2" class="com.hxzy.model.Person">
<property name="age" value="58">
</property>
<property name="name" value="老刘"></property>
以下红色部分可以使用这句话替换
<property name="cars" ref="cars">
<property name="cars">
<list>
<ref bean="car" />
<ref bean="car2" />
</list>
</property>
</bean>
<bean id="person4" class="com.hxzy.model.Person">
<property name="age" value="48"></property>
<property name="name" value="夏洛"></property>
<!--此处调用了外部的cars bean-->
<property name="cars" ref="cars"></property>
</bean>
<!--此处即为抽出的cars集合bean-->
<util:list id="cars">
<ref bean="car" />
<ref bean="car2" />
</util:list>
<bean id="dataSource" class="com.hxzy.model.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">1234</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
</beans>