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

相关文章

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

uva 11178 计算集合模板题

题意: 求三角形行三个角三等分点射线交出的内三角形坐标。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <

滚雪球学Java(87):Java事务处理:JDBC的ACID属性与实战技巧!真有两下子!

咦咦咦,各位小可爱,我是你们的好伙伴——bug菌,今天又来给大家普及Java SE啦,别躲起来啊,听我讲干货还不快点赞,赞多了我就有动力讲得更嗨啦!所以呀,养成先点赞后阅读的好习惯,别被干货淹没了哦~ 🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,助你一臂之力,带你早日登顶🚀,欢迎大家关注&&收藏!持续更新中,up!up!up!! 环境说明:Windows 10

自定义类型:结构体(续)

目录 一. 结构体的内存对齐 1.1 为什么存在内存对齐? 1.2 修改默认对齐数 二. 结构体传参 三. 结构体实现位段 一. 结构体的内存对齐 在前面的文章里我们已经讲过一部分的内存对齐的知识,并举出了两个例子,我们再举出两个例子继续说明: struct S3{double a;int b;char c;};int mian(){printf("%zd\n",s

【编程底层思考】垃圾收集机制,GC算法,垃圾收集器类型概述

Java的垃圾收集(Garbage Collection,GC)机制是Java语言的一大特色,它负责自动管理内存的回收,释放不再使用的对象所占用的内存。以下是对Java垃圾收集机制的详细介绍: 一、垃圾收集机制概述: 对象存活判断:垃圾收集器定期检查堆内存中的对象,判断哪些对象是“垃圾”,即不再被任何引用链直接或间接引用的对象。内存回收:将判断为垃圾的对象占用的内存进行回收,以便重新使用。

flume系列之:查看flume系统日志、查看统计flume日志类型、查看flume日志

遍历指定目录下多个文件查找指定内容 服务器系统日志会记录flume相关日志 cat /var/log/messages |grep -i oom 查找系统日志中关于flume的指定日志 import osdef search_string_in_files(directory, search_string):count = 0

Spring 源码解读:自定义实现Bean定义的注册与解析

引言 在Spring框架中,Bean的注册与解析是整个依赖注入流程的核心步骤。通过Bean定义,Spring容器知道如何创建、配置和管理每个Bean实例。本篇文章将通过实现一个简化版的Bean定义注册与解析机制,帮助你理解Spring框架背后的设计逻辑。我们还将对比Spring中的BeanDefinition和BeanDefinitionRegistry,以全面掌握Bean注册和解析的核心原理。

两个月冲刺软考——访问位与修改位的题型(淘汰哪一页);内聚的类型;关于码制的知识点;地址映射的相关内容

1.访问位与修改位的题型(淘汰哪一页) 访问位:为1时表示在内存期间被访问过,为0时表示未被访问;修改位:为1时表示该页面自从被装入内存后被修改过,为0时表示未修改过。 置换页面时,最先置换访问位和修改位为00的,其次是01(没被访问但被修改过)的,之后是10(被访问了但没被修改过),最后是11。 2.内聚的类型 功能内聚:完成一个单一功能,各个部分协同工作,缺一不可。 顺序内聚:

Mysql BLOB类型介绍

BLOB类型的字段用于存储二进制数据 在MySQL中,BLOB类型,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储的大小不同。 TinyBlob 最大 255 Blob 最大 65K MediumBlob 最大 16M LongBlob 最大 4G

Oracle type (自定义类型的使用)

oracle - type   type定义: oracle中自定义数据类型 oracle中有基本的数据类型,如number,varchar2,date,numeric,float....但有时候我们需要特殊的格式, 如将name定义为(firstname,lastname)的形式,我们想把这个作为一个表的一列看待,这时候就要我们自己定义一个数据类型 格式 :create or repla