Spring6梳理13——依赖注入之引入集合Bean属性

以上笔记来源:
尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc

13  依赖注入之引入集合Bean属性

13.1  创建Lesson类,student类和teacher实体类

由于1个学生对于多个课程,因此在学生实体类中创建lessonList的集合变量,并且在学生实体类中创建教师Map集合变量

package com.atguigu.spring6.iocxml.dimap;

/**
 * @package: com.atguigu.spring6.iocxml.dimap
 * @className: Lesson
 * @Description:
 * @author: haozihua
 * @date: 2024/9/26 14:39
 */
public class Lesson {
    private String lessonName;

    public String getLessonName() {
        return lessonName;
    }

    public void setLessonName(String lessonName) {
        this.lessonName = lessonName;
    }

    @Override
    public String toString() {
        return "Lesson{" +
                "lessonName='" + lessonName + '\'' +
                '}';
    }
}
package com.atguigu.spring6.iocxml.dimap;

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

/**
 * @package: com.atguigu.spring6.iocxml.dimap
 * @className: Student
 * @Description:
 * @author: haozihua
 * @date: 2024/8/20 15:20
 */
public class Student {

    private List<Lesson> lessonList;
    //一个学生对应很多个老师
    private Map<String,Teacher> teacherMap;

    private String sid;
    private String sname;

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

    public void setLessonList(List<Lesson> lessonList) {
        this.lessonList = 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> getTeacherMap() {
        return teacherMap;
    }

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


    public void run() {
        System.out.println("学生编号: " + sid + " " + "学生名称:" + sname);
        System.out.println(teacherMap);
        for (String teacher : teacherMap.keySet()) {
            System.out.println(teacher);
        }
        for (Teacher teacher : teacherMap.values()) {
            System.out.println(teacher);
        }
    }
}
package com.atguigu.spring6.iocxml.dimap;

/**
 * @package: com.atguigu.spring6.iocxml.dimap
 * @className: Teacher
 * @Description:
 * @author: haozihua
 * @date: 2024/8/20 15:21
 */
public class Teacher {
        private Integer teacherId;
        private String teacherName;

        public Integer getTeacherId() {
                return teacherId;
        }

        public void setTeacherId(Integer teacherId) {
                this.teacherId = teacherId;
        }

        public String getTeacherName() {
                return teacherName;
        }

        public void setTeacherName(String teacherName) {
                this.teacherName = teacherName;
        }

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

        public Teacher(Integer teacherId, String teacherName) {
                this.teacherId = teacherId;
                this.teacherName = teacherName;
        }
        public Teacher() {

        }

}

13.2  创建XML配置文件

在使用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">

之后,可以将Property属性中name属性以及<list>,<map>标签转换为util:list以及util:map标签,从而简化注入流程。
之后将util:list>id>ref bean标签以及util:map>id>entry>key>value或者key>ref设置好

<?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">
    <!--
      1.创建三个对象
      2.注入普通类型属性
      3.使用util:类型 定义
      4.在学生bean引入util:类型定义bean,完成list,map类型的注入-->
    <bean id="student" class="com.atguigu.spring6.iocxml.dimap.Student">
        <property name="sid" value="10000"></property>
        <property name="sname" value="lucy"></property>
        <property name="lessonList" ref="lessonList"></property>
        <property name="teacherMap" ref="teacherMap"></property>

    </bean>
    <util:list id="lessonList">
        <ref bean="lessonone"></ref>
        <ref bean="lessontwo"></ref>
    </util:list>

    <util:map id="teacherMap">
        <entry>
            <key>
                <value>10010</value>
            </key>
            <ref bean="teacherone"></ref>
        </entry>

        <entry>
            <key>
                <value>10011</value>
            </key>
            <ref bean="teachertwo"></ref>
        </entry>
    </util:map>

    <bean id="lessonone" class="com.atguigu.spring6.iocxml.dimap.Lesson">
        <property name="lessonName" value="java开发"></property>
    </bean>
    <bean id="lessontwo" class="com.atguigu.spring6.iocxml.dimap.Lesson">
        <property name="lessonName" value="python开发"></property>
    </bean>

    <bean id="teacherone" class="com.atguigu.spring6.iocxml.dimap.Teacher">
        <property name="teacherId" value="101"></property>
        <property name="teacherName" value="贾宝玉"></property>
    </bean>
    <bean id="teachertwo" class="com.atguigu.spring6.iocxml.dimap.Teacher">
        <property name="teacherId" value="102"></property>
        <property name="teacherName" value="李时珍"></property>
    </bean>

</beans>

13.3  运行测试类方法

    @Test
    public void Testp(){
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean-diref.xml");
        Student student = context.getBean("student",Student.class);
        student.run();
    }

13.4  运行结果如图所示 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值