spring学习(二)(当属性有list、map时的xml文件配置)

当我们有些类要交给spring来管理,这些类中又有一些类型为list或map的属性需要在配置项中初始化时,则如何配置?具体如下:

我的配置都是基于以下实体类

实体类:

package com.yc.spring02;

import java.util.List;
import java.util.Map;

public class Collection {
    private List<String> list;
    private Map<String, String> map;
    private List<Map<String, String>> list2;
    
    private List<Student> students;
    
    public Collection(){
        
    }
    
    
    public Collection(List<String> list, Map<String, String> map, List<Map<String, String>> list2,
            List<Student> students) {
        super();
        this.list = list;
        this.map = map;
        this.list2 = list2;
        this.students = students;
    }
    public List<Student> getStudents() {
        return students;
    }
    public void setStudents(List<Student> students) {
        this.students = students;
    }
    public List<String> getList() {
        return list;
    }
    public void setList(List<String> list) {
        this.list = list;
    }
    public Map<String, String> getMap() {
        return map;
    }
    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public List<Map<String, String>> getList2() {
        return list2;
    }
    public void setList2(List<Map<String, String>> list2) {
        this.list2 = list2;
    }
    @Override
    public String toString() {
        return "Collection [list=" + list + ", map=" + map + ", list2=" + list2 + ", students=" + students + "]";
    }
    
    
}
Collection.java

配置如下:

<?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.xsd">

    <!-- 给list注值 -->
    <bean id="collection01" class="com.yc.spring02.Collection">
        <property name="list"><!-- List<String>配置 -->
            <list>
                <value>张三</value>
                <value>李四</value>
                <value>王五</value>
            </list>
        </property>
        <property name="map"><!-- Map<String, String>配置 -->
            <map>
                <entry key="name" value="yc"></entry>
                <entry key="age" value="18"></entry>
                <entry key="sex" value="男"></entry>
            </map>
        </property>
        
        <property name="list2"><!-- List<Map<String,String>>配置 -->
            <list>
                <map>
                    <entry key="男" value="周杰伦"></entry>
                    <entry key="女" value="蔡依林"></entry>
                </map>
            
                <map>
                    <entry key="1" value="啦啦啦"></entry>
                    <entry key="2" value="巴拉巴拉"></entry>
                </map>
            </list>
        </property>
        
        <property name="students"><!-- List<Student>配置 -->
            <list>
                <bean class="com.yc.spring02.Student">
                    <property name="sid" value="yc001"></property>
                     <property name="name" value="yc"></property>
                     <property name="sex" value="男"></property>
                </bean>
            
                <bean class="com.yc.spring02.Student">
                    <property name="sid" value="yc002"></property>
                     <property name="name" value="yc2"></property>
                     <property name="sex" value="男"></property>
                </bean>
                <ref  bean="refList"/><!-- 引用外界的bean -->
            </list>
        </property>
    </bean>
    
    <bean id="refList" class="com.yc.spring02.Student">
        <property name="sid" value="yc003"></property>
        <property name="name" value="yc3"></property>
        <property name="sex" value="男"></property>
    </bean>
            
</beans>