使用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();
}
}
运行结果: