spring-注入list集合对象(值是对象)

1.创建stu类

public class Stu {
     
//    //1.数组类型
//    private String[] courses;
//
//    //2.list集合属性
//    private List list;
//
//    //3.map集合类型
//    private Map map;
//
//    //4.set集合属性类型
//    private Set set;

    private List<Course> courseList;

    public void setCourseList(List<Course> courseList) {
     
        this.courseList = courseList;
    }

//    public void setSet(Set set) {
     
//        this.set = set;
//    }
//
//    public void setMap(Map map) {
     
//        this.map = map;
//    }
//
//    public void setList(List list) {
     
//        this.list = list;
//    }
//
//    public void setCourses(String[] courses) {
     
//        this.courses = courses;
//    }
//    public void test(){
     
//        System.out.println(Arrays.toString(courses));
//        System.out.println(list);
//        System.out.println(map);
//        System.out.println(set);
//    }
}

2.创建Course类

public class Course {
     
    private String cname;

    public void setCname(String cname) {
     
        this.cname = cname;
    }
}

3.配置bean1.xml


<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">
    <bean name="stu" class="com.spring.collections.Stu">
        



























        
        <property name="courseList">
            <list>
                <ref bean="course1">ref>
                <ref bean="course2">ref>
            list>
        property>

    bean>


    <bean id="course1" class="com.spring.collections.Course">
        <property name="cname" value="Spring5">property>
    bean>
    <bean id="course2" class="com.spring.collections.Course">
        <property name="cname" value="MyBatis">property>
    bean>
beans>

你可能感兴趣的:(Spring,spring,ioc,java)