Spring6梳理12——依赖注入之注入Map集合类型属性
以上笔记来源:
尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc
12 依赖注入之注入Map集合类型属性
12.1 创建Student类和Teacher类
Student类中创建了run方法,展示了被注入的普通属性和map属性的值
Teacher类则创建了map类型的getter,setter方法以及方便输出的tostring方法
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 Map<String,Teacher> teacherMap;private String sid;private String sname;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);}
}
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;}@Overridepublic String toString() {return "Teacher{" +"teacherId=" + teacherId +", teacherName='" + teacherName + '\'' +'}';}public Teacher(Integer teacherId, String teacherName) {this.teacherId = teacherId;this.teacherName = teacherName;}public Teacher() {}}
12.2 创建XML配置文件
由于在Student类中创建了关于Teacher类的map集合,因此在student的bean中配置信息,首先创建普通类型属性,根据实体类中的变量名称,设置name属性值
之后创建Map集合类型属性时,注意entry>key>value或者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"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="2000"></property><property name="sname" value="李四"></property><property name="teacherMap"><map><entry><key><value>10010</value></key><ref bean="teacherone"></ref></entry><entry><key><value>210</value></key><ref bean="teachertwo"></ref></entry></map></property></bean>
</beans>
12.3 创建测试类方法
@Testpublic void TestStu(){ApplicationContext context =new ClassPathXmlApplicationContext("bean-dimap.xml");Student student = context.getBean("student",Student.class);student.run();}