spring6 为集合类型属性赋值 --引用集合类型的bean
2023-12-16 20:30:40
1.准备工作:
Student.java
package bean.dimap;
import java.util.List;
import java.util.Map;
public class Student {
private String sid;
private String sname;
//
private Map<String,Teacher> map;
//
private List<Lesson> lessonList;
public List<Lesson> getLessonList() {
return lessonList;
}
public void setLessonList(List<Lesson> lessonList) {
this.lessonList = lessonList;
}
public void run(){
System.out.println("学生名:"+sname+" 学生id:"+sid);
System.out.println(map);
System.out.println(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> getMap() {
return map;
}
public void setMap(Map<String, Teacher> map) {
this.map = map;
}
@Override
public String toString() {
return "Student{" +
"sid='" + sid + '\'' +
", sname='" + sname + '\'' +
", map=" + map +
", lessonList=" + lessonList +
'}';
}
}
Teacher.java
package bean.dimap;
public class Teacher {
private String tId;
private String tName;
@Override
public String toString() {
return "Teacher{" +
"tId='" + tId + '\'' +
", 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;
}
}
2.xml配置:
beans-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">
<!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 -->
<bean id="student" class="bean.dimap.Student">
<property name="sname" value="hhh"></property>
<property name="sid" value="01"></property>
<property name="lessonList" ref="list"></property>
<property name="map" ref="map"></property>
</bean>
<!--list集合类型的bean-->
<util:list id="list">
<ref bean="lessonone"></ref>
<ref bean="lessontwo"></ref>
</util:list>
<!--map集合类型的bean-->
<util:map id="map">
<entry>
<key>
<value>001</value>
</key>
<ref bean="teacherone"></ref>
</entry>
<entry>
<key>
<value>002</value>
</key>
<ref bean="teachertwo"></ref>
</entry>
</util:map>
<bean id="lessonone" class="bean.dimap.Lesson">
<property name="lessonName" value="java课"></property>
</bean>
<bean id="lessontwo" class="bean.dimap.Lesson">
<property name="lessonName" value="c++课"></property>
</bean>
<bean id="teacherone" class="bean.dimap.Teacher">
<property name="tName" value="张老师"></property>
<property name="tId" value="001"></property>
</bean>
<bean id="teachertwo" class="bean.dimap.Teacher">
<property name="tName" value="李老师"></property>
<property name="tId" value="002"></property>
</bean>
</beans>
3.前置知识:使用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">
</beans>
4.控制台输出?
文章来源:https://blog.csdn.net/qq_64005599/article/details/135036460
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!