spring6 为集合类型属性赋值 --引用集合类型的bean

2023-12-16 20:36

本文主要是介绍spring6 为集合类型属性赋值 --引用集合类型的bean,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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;}@Overridepublic 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;@Overridepublic 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/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://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/utilhttp://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>

4.控制台输出 

这篇关于spring6 为集合类型属性赋值 --引用集合类型的bean的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/501809

相关文章

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

python删除xml中的w:ascii属性的步骤

《python删除xml中的w:ascii属性的步骤》使用xml.etree.ElementTree删除WordXML中w:ascii属性,需注册命名空间并定位rFonts元素,通过del操作删除属... 可以使用python的XML.etree.ElementTree模块通过以下步骤删除XML中的w:as

Spring如何使用注解@DependsOn控制Bean加载顺序

《Spring如何使用注解@DependsOn控制Bean加载顺序》:本文主要介绍Spring如何使用注解@DependsOn控制Bean加载顺序,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录1.javascript 前言2. 代码实现总结1. 前言默认情况下,Spring加载Bean的顺

SpringMVC高效获取JavaBean对象指南

《SpringMVC高效获取JavaBean对象指南》SpringMVC通过数据绑定自动将请求参数映射到JavaBean,支持表单、URL及JSON数据,需用@ModelAttribute、@Requ... 目录Spring MVC 获取 JavaBean 对象指南核心机制:数据绑定实现步骤1. 定义 Ja

Python打印对象所有属性和值的方法小结

《Python打印对象所有属性和值的方法小结》在Python开发过程中,调试代码时经常需要查看对象的当前状态,也就是对象的所有属性和对应的值,然而,Python并没有像PHP的print_r那样直接提... 目录python中打印对象所有属性和值的方法实现步骤1. 使用vars()和pprint()2. 使

CSS3中的字体及相关属性详解

《CSS3中的字体及相关属性详解》:本文主要介绍了CSS3中的字体及相关属性,详细内容请阅读本文,希望能对你有所帮助... 字体网页字体的三个来源:用户机器上安装的字体,放心使用。保存在第三方网站上的字体,例如Typekit和Google,可以link标签链接到你的页面上。保存在你自己Web服务器上的字

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

Spring中管理bean对象的方式(专业级说明)

《Spring中管理bean对象的方式(专业级说明)》在Spring框架中,Bean的管理是核心功能,主要通过IoC(控制反转)容器实现,下面给大家介绍Spring中管理bean对象的方式,感兴趣的朋... 目录1.Bean的声明与注册1.1 基于XML配置1.2 基于注解(主流方式)1.3 基于Java