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

相关文章

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

MySQL 事务的概念及ACID属性和使用详解

《MySQL事务的概念及ACID属性和使用详解》MySQL通过多线程实现存储工作,因此在并发访问场景中,事务确保了数据操作的一致性和可靠性,下面通过本文给大家介绍MySQL事务的概念及ACID属性和... 目录一、什么是事务二、事务的属性及使用2.1 事务的 ACID 属性2.2 为什么存在事务2.3 事务

Spring Cache注解@Cacheable的九个属性详解

《SpringCache注解@Cacheable的九个属性详解》在@Cacheable注解的使用中,共有9个属性供我们来使用,这9个属性分别是:value、cacheNames、key、key... 目录1.value/cacheNames 属性2.key属性3.keyGeneratjavascriptor

Spring Boot 事务详解(事务传播行为、事务属性)

《SpringBoot事务详解(事务传播行为、事务属性)》SpringBoot提供了强大的事务管理功能,通过@Transactional注解可以方便地配置事务的传播行为和属性,本文将详细介绍Spr... 目录Spring Boot 事务详解引言声明式事务管理示例编程式事务管理示例事务传播行为1. REQUI

Java资源管理和引用体系的使用详解

《Java资源管理和引用体系的使用详解》:本文主要介绍Java资源管理和引用体系的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Java的引用体系1、强引用 (Strong Reference)2、软引用 (Soft Reference)3、弱引用 (W

SpringIOC容器Bean初始化和销毁回调方式

《SpringIOC容器Bean初始化和销毁回调方式》:本文主要介绍SpringIOC容器Bean初始化和销毁回调方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录前言1.@Bean指定初始化和销毁方法2.实现接口3.使用jsR250总结前言Spring Bea