在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.
配置 java.util.List 类型的属性, 需要指定 <list> 标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.
数组的定义和 List 一样, 都使用 <list>
配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.
Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值.
必须在 <key> 标签里定义键
因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素.
可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义
使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性.
定义Java类
package xyz.huning.spring4.di; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; public class Student { private int id; private String[] teacherNameArray; private List<String> teacherNameList; private Set<String> teacherNameSet; private Map<Integer,String> teacherNameMap; private Teacher[] teacherArray; private List<Teacher> teacherList; private Set<Teacher> teacherSet; private Map<String,Teacher> teacherMap; private Properties properties; public int getId() { return id; } public void setId(int id) { this.id = id; } public String[] getTeacherNameArray() { return teacherNameArray; } public void setTeacherNameArray(String[] teacherNameArray) { this.teacherNameArray = teacherNameArray; } public List<String> getTeacherNameList() { return teacherNameList; } public void setTeacherNameList(List<String> teacherNameList) { this.teacherNameList = teacherNameList; } public Set<String> getTeacherNameSet() { return teacherNameSet; } public void setTeacherNameSet(Set<String> teacherNameSet) { this.teacherNameSet = teacherNameSet; } public Map<Integer, String> getTeacherNameMap() { return teacherNameMap; } public void setTeacherNameMap(Map<Integer, String> teacherNameMap) { this.teacherNameMap = teacherNameMap; } public Teacher[] getTeacherArray() { return teacherArray; } public void setTeacherArray(Teacher[] teacherArray) { this.teacherArray = teacherArray; } public List<Teacher> getTeacherList() { return teacherList; } public void setTeacherList(List<Teacher> teacherList) { this.teacherList = teacherList; } public Set<Teacher> getTeacherSet() { return teacherSet; } public void setTeacherSet(Set<Teacher> teacherSet) { this.teacherSet = teacherSet; } public Map<String, Teacher> getTeacherMap() { return teacherMap; } public void setTeacherMap(Map<String, Teacher> teacherMap) { this.teacherMap = teacherMap; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Student [id=" + id + ", teacherNameArray=" + Arrays.toString(teacherNameArray) + ", teacherNameList=" + teacherNameList + ", teacherNameSet=" + teacherNameSet + ", teacherNameMap=" + teacherNameMap + ", teacherArray=" + Arrays.toString(teacherArray) + ", teacherList=" + teacherList + ", teacherSet=" + teacherSet + ", teacherMap=" + teacherMap + ", properties=" + properties + "]"; } }
package xyz.huning.spring4.di; public class Teacher { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Teacher [id=" + id + ", name=" + name + "]"; } }
<!--配置一个 bean--> <bean id="teacher1" class="xyz.huning.spring4.di.Teacher"> <property name="id" value="1"></property> <property name="name" value="LiJun"></property> </bean> <!--配置一个 bean--> <bean id="teacher2" class="xyz.huning.spring4.di.Teacher"> <property name="id" value="2"></property> <property name="name" value="WangTian"></property> </bean> <!--配置一个 bean--> <bean id="teacher3" class="xyz.huning.spring4.di.Teacher"> <property name="id" value="3"></property> <property name="name" value="ChenXin"></property> </bean> <!--配置一个 bean--> <!--基本类型的数组--> <bean id="student1" class="xyz.huning.spring4.di.Student"> <property name="id" value="1"></property> <property name="teacherNameArray"> <array> <value>LiJun</value> <value>WangTian</value> <value>ChenXin</value> </array> </property> </bean> <!--配置一个 bean--> <!--基本类型的List--> <bean id="student2" class="xyz.huning.spring4.di.Student"> <property name="id" value="2"></property> <property name="teacherNameList"> <list> <value>LiJun</value> <value>WangTian</value> <value>ChenXin</value> </list> </property> </bean> <!--配置一个 bean--> <!--基本类型的Set--> <bean id="student3" class="xyz.huning.spring4.di.Student"> <property name="id" value="3"></property> <property name="teacherNameSet"> <set> <value>LiJun</value> <value>WangTian</value> <value>ChenXin</value> </set> </property> </bean> <!--配置一个 bean--> <!--基本类型的Map--> <bean id="student4" class="xyz.huning.spring4.di.Student"> <property name="id" value="4"></property> <property name="teacherNameMap"> <map> <entry key="1" value="LiJun"></entry> <entry key="2" value="WangTian"></entry> <entry key="3" value="ChenXin"></entry> </map> </property> </bean> <!--配置一个 bean--> <!--对象类型的数组--> <bean id="student5" class="xyz.huning.spring4.di.Student"> <property name="id" value="5"></property> <property name="teacherArray"> <array> <ref bean="teacher1"/> <ref bean="teacher2"/> <ref bean="teacher3"/> </array> </property> </bean> <!--配置一个 bean--> <!--对象类型的List--> <bean id="student6" class="xyz.huning.spring4.di.Student"> <property name="id" value="6"></property> <property name="teacherList"> <list> <ref bean="teacher1"/> <ref bean="teacher2"/> <ref bean="teacher3"/> </list> </property> </bean> <!--配置一个 bean--> <!--对象类型的Set--> <bean id="student7" class="xyz.huning.spring4.di.Student"> <property name="id" value="7"></property> <property name="teacherSet"> <set> <ref bean="teacher1"/> <ref bean="teacher2"/> <ref bean="teacher3"/> </set> </property> </bean> <!--配置一个 bean--> <!--对象类型的Map--> <bean id="student8" class="xyz.huning.spring4.di.Student"> <property name="id" value="8"></property> <property name="teacherMap"> <map> <entry key="1" value-ref="teacher1"></entry> <entry key="2" value-ref="teacher2"></entry> <entry key="3" value-ref="teacher3"></entry> </map> </property> </bean> <!--配置一个 bean--> <!--Properties类型--> <bean id="student9" class="xyz.huning.spring4.di.Student"> <property name="id" value="9"></property> <property name="properties"> <props> <prop key="name">huning</prop> <prop key="gender">male</prop> <prop key="length">170</prop> </props> </property> </bean>