spring6 为集合类型属性赋值 --引用集合类型的bean

本文详细介绍了如何在Spring框架中使用XML配置文件进行Bean的定义、依赖注入,包括Student、Teacher类的实例化以及利用util:list和util:map进行集合类型的bean引用。
摘要由CSDN通过智能技术生成

1.准备工作:

Student.java

package bean.dimap;

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

public class Student {

    private String sid;
    private String sname;
//
    private Map<String,Teacher> map;
    //
    private List<Lesson> lessonList;

    public List<Lesson> getLessonList() {
        return lessonList;
    }

    public void setLessonList(List<Lesson> lessonList) {
        this.lessonList = lessonList;
    }

    public void run(){
        System.out.println("学生名:"+sname+" 学生id:"+sid);
        System.out.println(map);
        System.out.println(lessonList);
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Map<String, Teacher> getMap() {
        return map;
    }

    public void setMap(Map<String, Teacher> map) {
        this.map = map;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid='" + sid + '\'' +
                ", sname='" + sname + '\'' +
                ", map=" + map +
                ", lessonList=" + lessonList +
                '}';
    }
}

Teacher.java

package bean.dimap;

public class Teacher {
    private String tId;
    private String tName;

    @Override
    public String toString() {
        return "Teacher{" +
                "tId='" + tId + '\'' +
                ", tName='" + tName + '\'' +
                '}';
    }

    public String gettId() {
        return tId;
    }

    public void settId(String tId) {
        this.tId = tId;
    }

    public String gettName() {
        return tName;
    }

    public void settName(String tName) {
        this.tName = tName;
    }
}

2.xml配置:

beans-diRef.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/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
 <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
    <bean id="student" class="bean.dimap.Student">
        <property name="sname" value="hhh"></property>
        <property name="sid" value="01"></property>

        <property name="lessonList" ref="list"></property>
        <property name="map" ref="map"></property>
    </bean>

<!--list集合类型的bean-->
    <util:list id="list">
        <ref bean="lessonone"></ref>
        <ref bean="lessontwo"></ref>
    </util:list>

<!--map集合类型的bean-->
    <util:map id="map">
        <entry>
            <key>
                <value>001</value>
            </key>
            <ref bean="teacherone"></ref>
        </entry>
        <entry>
            <key>
                <value>002</value>
            </key>
            <ref bean="teachertwo"></ref>
        </entry>
    </util:map>

    <bean id="lessonone" class="bean.dimap.Lesson">
        <property name="lessonName" value="java课"></property>
    </bean>
    <bean id="lessontwo" class="bean.dimap.Lesson">
        <property name="lessonName" value="c++课"></property>
    </bean>
    <bean id="teacherone" class="bean.dimap.Teacher">
        <property name="tName" value="张老师"></property>
        <property name="tId" value="001"></property>
    </bean>
    <bean id="teachertwo" class="bean.dimap.Teacher">
        <property name="tName" value="李老师"></property>
        <property name="tId" value="002"></property>
    </bean>


</beans>

3.前置知识:使用util:list、util:map标签必须引入相应的命名空间

<?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/util
    http://www.springframework.org/schema/util/spring-util.xsd
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

4.控制台输出 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值