当前位置: 首页 > news >正文

Spring系统学习 -Spring IOC 的XML管理Bean之类类型属性赋值、数组类型属性赋值、集合类属性赋值

  • 类类型属性赋值: 在XML配置中,可以使用 <property> 元素为类类型属性赋值。通过指定属性的名称和值,可以直接将其他Bean的实例引用注入到目标Bean的属性中。这种方式可以建立对象之间的关联关系。例如,可以将一个Address对象注入到Person对象的address属性中。

  • 数组类型属性赋值: XML配置中,可以使用 <property> 元素为数组类型属性赋值。通过使用 <array><list> 元素,可以定义一个数组或列表,并使用 <value> 元素指定数组或列表的元素值。这样可以将多个值一次性赋给数组类型的属性。

  • 集合类型属性赋值: 在XML配置中,可以使用 <property> 元素为集合类型属性赋值。通过使用 <list><set><map><props> 元素,可以定义一个列表、集合、映射或属性集,并使用相应的元素指定集合的元素值。这样可以一次性赋给集合类型的属性。

类类型属性赋值

类类型属性赋值是指将一个类的实例赋值给另一个类的属性。在Java中,类类型属性是指一个类中的属性的类型是另一个类。

我们创建如下类:

public class Clazz {private Integer clazzId;private String clazzName;public Integer getClazzId() {return clazzId;}public void setClazzId(Integer clazzId) {this.clazzId = clazzId;}public String getClazzName() {return clazzName;}public void setClazzName(String clazzName) {this.clazzName = clazzName;}@Overridepublic String toString() {return "Clazz{" +"clazzId=" + clazzId +", clazzName='" + clazzName + '\'' +'}';}public Clazz() {}public Clazz(Integer clazzId, String clazzName) {this.clazzId = clazzId;this.clazzName = clazzName;}
}

然后创建一个Student类

public class student{private Integer stundentId;private String studentName;private Clazz clazz;public Integer getStundentId() {return stundentId;}public void setStundentId(Integer stundentId) {this.stundentId= stundentId;}public String getStudentName() {return clazzName;}public void setClazzName(String studentName) {this.studentName= studentName;}public Clazz getClazz() {return clazz;}public void setClazz(Clazz clazz) {this.clazz = clazz;}
}

方式一: 引用外部已经声明的bean

这个是在我们的spring的applicationContext.xml文件中配置的

配置Clazz类型的bean:

<bean id="clazzOne" class="com.miaow.spring.bean.Clazz"><property name="clazzId" value="1111"></property><property name="clazzName" value="财源滚滚班"></property>
</bean>

为Student中的Clazz属性赋值:

<bean id="studentFour" class="com.miaow.spring.bean.Student"><property name="studentId" value="1004"></property><property name="studentName" value="赵六"></property><!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><property name="clazz" ref="clazzOne"></property>
</bean>

错误演示

<bean id="studentFour" class="com.miaow.spring.bean.Student"><property name="studentId" value="1004"></property><property name="studentName" value="赵六"></property><property name="clazz" value="clazzOne"></property>
</bean>

如果错把ref属性写成了value属性,会抛出异常: Caused by: java.lang.IllegalStateException:Cannot convert value of type ‘java.lang.String’ to required type ‘com.miaow.spring.bean.Clazz’ for property ‘clazz’: no matching editors or conversion strategy found
意思是不能把String类型转换成我们要的Clazz类型,说明我们使用value属性时,Spring只把这个属性看做一个普通的字符串,不会认为这是一个bean的id,更不会根据它去找到bean来赋值

内部bean的方式

同样的在applicationContext.xml文件中配置如下代码:

<bean id="studentFour" class="com.miaow.spring.bean.Student"><property name="studentId" value="1004"></property><property name="studentName" value="赵六"></property><property name="clazz"><!-- 在一个bean中再声明一个bean就是内部bean --><!-- 内部bean只能用于给属性赋值,不能在外部通过IOC容器获取,因此可以省略id属性 --><bean id="clazzInner" class="com.miaow.spring.bean.Clazz"><property name="clazzId" value="2222"></property><property name="clazzName" value="远大前程班"></property></bean></property>
</bean>

首先,通过 <bean> 标签定义了一个名为 studentFour 的Bean,并指定了它的类为 com.miaow.spring.bean.Student。

<bean> 标签中,使用 <property> 标签为 studentFour Bean 的属性赋值。其中,name 属性指定了要赋值的属性名,value 属性指定了要赋的值。

<property> 标签中,使用 <bean> 标签定义了一个内部Bean,即 clazzInner。内部Bean的类为 com.miaow.spring.bean.Clazz。

<bean> 标签中,使用 <property> 标签为 clazzInner Bean 的属性赋值。同样,使用 name 属性指定了要赋值的属性名,value 属性指定了要赋的值。

级联属性赋值

在Spring中,级联属性赋值是指在给一个对象的属性赋值时,同时给该对象的属性的属性(即子属性)赋值。这样可以方便地一次性设置多层嵌套属性的值。

<bean id="studentFour" class="com.miaow.spring.bean.Student"><property name="studentId" value="1004"></property><property name="studentName" value="赵六"></property><!-- 一定先引用某个bean为属性赋值,才可以使用级联方式更新属性 --><property name="clazz" ref="clazzOne"></property><property name="clazz.clazzId" value="3333"></property><property name="clazz.clazzName" value="最强王者班"></property>
</bean>

首先,通过 <bean> 标签定义了一个名为 studentFour 的Bean,并指定了它的类为 com.miaow.spring.bean.Student。
然后,使用 <property> 标签为 studentFour Bean 的属性赋值。其中,name 属性指定了要赋值的属性名,value 属性指定了要赋的值。
接下来,通过 <property> 标签引用了一个名为 clazzOne 的Bean,并将其赋值给 studentFour 的 clazz 属性。这是级联属性赋值的前提,需要先引用某个Bean为属性赋值。
最后,使用级联属性赋值的方式,通过在 <property> 标签中使用点号 . 来表示级联属性的层级关系,设置了 studentFour 的 clazz 对象的 clazzId 和 clazzName 属性的值。

联机属性测试方法:

    @Testpublic void CascadeValueTest(){ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");Student student = (Student) context.getBean("studentFive");System.out.println(student);System.out.println(student.getClassz().toString());}

数组类型属性赋值的方式

在Spring的XML配置文件中,可以使用 或 元素为数组类型属性赋值。以下是两种常见的方式:

使用 <array> 元素:

在Student中添加了如下代码:

private String[] hobbies;
public String[] getHobbies() {return hobbies;
}
public void setHobbies(String[] hobbies) {this.hobbies = hobbies;
}

在XML文件中新增如下

<property name="hobbies"><array><value>吃饭</value><value>玩游戏</value><value>打麻将</value></array></property>

使用 <list> 进行赋值

<property name="hobbies"><list><value>吃饭</value><value>玩游戏</value><value>打麻将</value></list></property>

在上述示例中,通过 <list> 元素包裹多个<value>元素,每个 <value> 元素表示数组中的一个元素。

无论是使用 <list> 还是 <array> 元素,都可以在其中添加多个 <value> 元素来定义数组的元素。每个 <value> 元素的内容即为数组的一个元素的值。

为集合类型属性赋值

为List集合类型属性赋值

在Clazz中添加如下代码:

private List<Student> students;
public List<Student> getStudents() {return students;
}
public void setStudents(List<Student> students) {this.students = students;
}

配置bean:

<bean id="clazzTwo" class="com.miaow.spring.bean.Clazz"><property name="clazzId" value="4444"></property><property name="clazzName" value="Javaee0222"></property><property name="students"><list><ref bean="studentOne"></ref><ref bean="studentTwo"></ref><ref bean="studentThree"></ref></list></property>
</bean><bean id="studentOne" class="com.miaow.spring.bean.Student"><property name="studentId" value="1004"></property><property name="studentName" value="赵六"></property>  
</bean>
<bean id="studentTwo" class="com.miaow.spring.bean.Student"><property name="studentId" value="1005"></property><property name="studentName" value="赵七"></property>  
</bean>
<bean id="studentThree" class="com.miaow.spring.bean.Student"><property name="studentId" value="1006"></property><property name="studentName" value="王五"></property>  
</bean>

若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可

为Map集合类型属性赋值

我们创建一个新类Teacher

public class Teacher {private Integer teacherId;private String teacherName;public Integer getTeacherId() {return teacherId;}public void setTeacherId(Integer teacherId) {this.teacherId = teacherId;}public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}public Teacher(Integer teacherId, String teacherName) {this.teacherId = teacherId;this.teacherName = teacherName;}public Teacher() {}@Overridepublic String toString() {return "Teacher{" +"teacherId=" + teacherId +", teacherName='" + teacherName + '\'' +'}';}
}

在Student类中添加如下代码:

private Map<String, Teacher> teacherMap;
public Map<String, Teacher> getTeacherMap() {return teacherMap;
}
public void setTeacherMap(Map<String, Teacher> teacherMap) {this.teacherMap = teacherMap;
}
在bean中配置
<bean id="teacherOne" class="com.miaow.spring.bean.Teacher"><property name="teacherId" value="10010"></property><property name="teacherName" value="大宝"></property>
</bean>
<bean id="teacherTwo" class="com.miaow.spring.bean.Teacher"><property name="teacherId" value="10086"></property><property name="teacherName" value="二宝"></property>
</bean>
<bean id="studentFour" class="com.miaow.spring.bean.Student"><property name="studentId" value="1004"></property><property name="studentName" value="赵六"></property>  <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><property name="clazz" ref="clazzOne"></property><property name="hobbies"><array><value>吃饭</value><value>玩游戏</value><value>打麻将</value></array></property><property name="teacherMap"><map><entry><key><value>10010</value></key><ref bean="teacherOne"></ref></entry><entry><key><value>10086</value></key><ref bean="teacherTwo"></ref></entry></map></property>
</bean>

在这里插入图片描述

引用集合类型的bean
<!--list集合类型的bean-->
<util:list id="students"><ref bean="studentOne"></ref><ref bean="studentTwo"></ref><ref bean="studentThree"></ref>
</util:list>
<!--map集合类型的bean-->
<util:map id="teacherMap"><entry><key><value>10010</value></key><ref bean="teacherOne"></ref></entry><entry><key><value>10086</value></key><ref bean="teacherTwo"></ref></entry>
</util:map>
<bean id="clazzTwo" class="com.miaow.spring.bean.Clazz"><property name="clazzId" value="4444"></property><property name="clazzName" value="Javaee0222"></property><property name="students" ref="students"></property>
</bean>
<bean id="studentFour" class="com.miaow.spring.bean.Student"><property name="studentId" value="1004"></property><property name="studentName" value="赵六"></property>  <!-- ref属性:引用IOC容器中某个bean的id,将所对应的bean为属性赋值 --><property name="clazz" ref="clazzOne"></property><property name="hobbies"><array><value>抽烟</value><value>喝酒</value><value>烫头</value></array></property><property name="teacherMap" ref="teacherMap"></property>
</bean>

使用util:list、util:map标签必须引入相应的命名空间,可以通过idea的提示功能选择

  • 引用顺序: 在使用 <util:list> 引用集合类型时,确保被引用的Bean已经在配置文件中定义并初始化。否则,可能会导致引用失败或引用到空集合。
  • 集合类型Bean的命名: 当使用 <util:list> 引用集合类型时,确保为集合类型的Bean提供唯一的ID。这样可以确保在引用时能够准确地指定要引用的Bean。
  • 集合类型Bean的作用域: 集合类型的Bean的作用域通常是单例(singleton)。确保在引用集合类型的Bean时,了解其作用域,并确保在适当的范围内引用。

相关文章:

  • PFA三颈平底烧瓶500ML四氟反应瓶透明可视耐酸碱腐蚀可定制
  • python游标卡尺什么梗
  • 【CT】LeetCode手撕—手撕快排
  • Hadoop 2.0:主流开源云架构(三)
  • MYSQL之MVCC多版本并发控制实现原理
  • TQZC706开发板教程:10G光口arp测试
  • 第四章 Three.js 绘制基本几何体
  • web移动前端网页:深度剖析与未来展望
  • Ubuntu 设置开机启动脚本
  • 【Pytorch】计算机视觉项目——卷积神经网络TinyVGG模型图像分类(如何使用自定义数据集)
  • 【人工智能】第三部分:ChatGPT的应用场景和挑战
  • 【教程】让小爱音箱Play增强版接入Mi-GPT
  • Android Jetpack Compose 实现一个电视剧选集界面
  • C#——方法函数详情
  • DNN模型介绍
  • 4个实用的微服务测试策略
  • canvas绘制圆角头像
  • ES2017异步函数现已正式可用
  • ES6系列(二)变量的解构赋值
  • exif信息对照
  • Js基础——数据类型之Null和Undefined
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • Meteor的表单提交:Form
  • PAT A1017 优先队列
  • Python学习之路16-使用API
  • React 快速上手 - 07 前端路由 react-router
  • RxJS: 简单入门
  • Spring Cloud Alibaba迁移指南(一):一行代码从 Hystrix 迁移到 Sentinel
  • Vue学习第二天
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 大整数乘法-表格法
  • 七牛云假注销小指南
  • 如何使用Mybatis第三方插件--PageHelper实现分页操作
  • 如何用Ubuntu和Xen来设置Kubernetes?
  • 使用API自动生成工具优化前端工作流
  • 数据结构java版之冒泡排序及优化
  • 详解NodeJs流之一
  • 小程序开发之路(一)
  • 这几个编码小技巧将令你 PHP 代码更加简洁
  • ionic入门之数据绑定显示-1
  • # centos7下FFmpeg环境部署记录
  • # 达梦数据库知识点
  • #define、const、typedef的差别
  • #stm32驱动外设模块总结w5500模块
  • #Z2294. 打印树的直径
  • (4)Elastix图像配准:3D图像
  • (4)STL算法之比较
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (附源码)ssm智慧社区管理系统 毕业设计 101635
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (一)搭建springboot+vue前后端分离项目--前端vue搭建
  • (原创)攻击方式学习之(4) - 拒绝服务(DOS/DDOS/DRDOS)
  • .NET : 在VS2008中计算代码度量值
  • .NET 8.0 发布到 IIS
  • .NET WPF 抖动动画