【Java】【Spring】xml管理Bean-依赖注入-引入集合bean

在这里插入图片描述
使用util:list、util:map标签必须引入相应的命名空间,主要注意xml配置。

Student类:

package com.kiong.spring6;

import java.io.StringReader;
import java.util.List;
import java.util.Map;

public class Student {
    private String sid;
    private String sname;
    private Map<String,Teacher> teacherMap;
    private List<Lesson> lessonList;

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

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

    public void printInfo(){
        System.out.println("学生编号:"+sid+"学生名称"+sname);
        System.out.println(teacherMap);
        System.out.println(lessonList);
    }

    public Student() {
    }

    public Student(String sid, String sname, Map<String, Teacher> teacherMap) {
        this.sid = sid;
        this.sname = sname;
        this.teacherMap = teacherMap;
    }

    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;
    }
}

Teacher类:

package com.kiong.spring6;

public class Teacher {
    private String tid;
    private String tname;

    public Teacher() {
    }

    public Teacher(String tid, String tname) {
        this.tid = tid;
        this.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;
    }

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

Lesson类

package com.kiong.spring6;

public class Lesson {
    private String lessonName;

    public Lesson(String lessonName) {
        this.lessonName = lessonName;
    }

    public Lesson() {
    }

    public String getLessonName() {
        return lessonName;
    }

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

    @Override
    public String toString() {
        return "Lesson{" +
                "lessonName='" + lessonName + '\'' +
                '}';
    }
}

bean-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">

    <bean id="student" class="com.kiong.spring6.Student">
        <property name="sid" value="21666"></property>
        <property name="sname" value="DingWei"></property>
    
        <!--注入list、map类型属性-->
        <property name="lessonList" ref="lessonList"></property>
        <property name="teacherMap" ref="teacherMap"></property>
    </bean>

    <util:list id="lessonList">
        <ref bean="lesson1"></ref>
        <ref bean="lesson2"></ref>
    </util:list>

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

    <bean id="lesson1" class="com.kiong.spring6.Lesson">
        <property name="lessonName" value="计算机组成原理"></property>
    </bean>
    
    <bean id="lesson2" class="com.kiong.spring6.Lesson">
        <property name="lessonName" value="数据结构"></property>
    </bean>
    
    <bean id="teacher1" class="com.kiong.spring6.Teacher">
        <property name="tid" value="200210"></property>
        <property name="tname" value="丁禾"></property>
    </bean>

    <bean id="teacher2" class="com.kiong.spring6.Teacher">
        <property name="tid" value="192012"></property>
        <property name="tname" value="李默"></property>
    </bean>

</beans>

TestStudent类:

package com.kiong.spring6;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestStudent {
    @Test
    public static void main(String[] args) {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean-diref.xml");
        Student student = context.getBean("student",Student.class);
        student.printInfo();
    }
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值