28.1 IOC容器基于XML管理的bean-依赖注入-Map类型数据
应用场景:创建一个教师类(Teacher),创建一个学生类(Student),通过XML文件进行依赖注入,使得学生类能够得到Map集合,并将Map集合中的信息输出到控制台
主要过程:
1.创建两个类
2.注入普通类型属性
3.在学生bean注入map集合类型属性
28.1.1 学生类
package com.atguigu.spring6.iocxml.dimap;
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 String sid;
private String sname;
private Map<String,Teacher> teacherMap;
public Map<String, Teacher> getTeacherMap() {
return teacherMap;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {
this.teacherMap = teacherMap;
}
@Override
public String toString() {
return "Student{" +
"sid='" + sid + '\'' +
", sname='" + sname + '\'' +
", 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 void run() {
System.out.println("学生编号: "+sid+" "+"学生名称:"+sname);
System.out.println(teacherMap);
}
}
28.1.2 教师类
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 + '\'' +
'}';
}
}
28.1.3 测试类
package com.atguigu.spring6.iocxml.dimap;
import com.atguigu.spring6.iocxml.dimap.Student;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @package: com.atguigu.spring6.iocxml.dimap
* @className: TestMap
* @Description:
* @author: haozihua
* @date: 2024/8/20 15:50
*/
public class TestMap {
@Test
public void TestStu(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean-dimap.xml");
Student student = context.getBean("student",Student.class);
student.run();
}
}
28.1.4 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
1.创建两个类
2.注入普通类型属性
3.在学生bean注入map集合类型属性
-->
<bean id="teacherone" class="com.atguigu.spring6.iocxml.dimap.Teacher">
<property name="teacherId" value="100"></property>
<property name="teacherName" value="西门庆"></property>
</bean>
<bean id="teachertwo" class="com.atguigu.spring6.iocxml.dimap.Teacher">
<property name="teacherId" value="200"></property>
<property name="teacherName" value="东方不败"></property>
</bean>
<bean id="student" class="com.atguigu.spring6.iocxml.dimap.Student">
<property name="sid" value="10000"></property>
<property name="sname" value="李四"></property>
<property name="teacherMap">
<map>
<entry>
<key>
<value>110</value>
</key>
<ref bean="teacherone"></ref>
</entry>
<entry>
<key>
<value>210</value>
</key>
<ref bean="teachertwo"></ref>
</entry>
</map>
</property>
</bean>
</beans>
图28-1 UML类图展示
图28-2 运行结果展示