util schema 定义集合:
- 使用基本的集合标签定义集合时 , 不能将集合作为独立的 Bean 定义 , 导致其他 Bean 无法引用该集合 , 所以无法在不同 Bean 之间共享集合。
- 可以用util schema 里的集合标签定义独立的集合 Bean。 需要注意的是 , 必须在 <beans> 根元素里添加 util schema 定义(xmlns:util="http://www.springframework.org/schema/util)。引入util命名空间。代码如下:
spring.xml文件的代码如下:
<?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: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-4.0.xsd"> <bean id="car" class="Car"> <constructor-arg value="baoma" ></constructor-arg> <constructor-arg value="12" ></constructor-arg> <constructor-arg value="red" ></constructor-arg> </bean> <!--用构造器去配置属性--> <bean id="car1" class="Car"> <constructor-arg value="bentian"></constructor-arg> <constructor-arg value="13"></constructor-arg> <constructor-arg value="blue"></constructor-arg> </bean> <!--配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间--> <util:list id="cars"> <ref bean="car"/> <ref bean="car1"/> </util:list> <bean id="person" class="collection.Person"> <property name="name" value="chen"></property> <property name="age" value="56"></property> <property name="cars" ref="cars"/> </bean> </beans>
person类的文件:
package collection;
import java.util.List;
import java.util.Map;
public class Person {
public String name;
public int age;
public List getCars() {
return cars;
}
public void setCars(List cars) {
this.cars = cars;
}
public List cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public Strin