spring 学习(spring-Dl补充(注入不同类型的数据))

前言

在之前的案例,列举的最多的是注入 对象。本篇博客则是补充说我们不仅可以注入对象 还可以注入其他的数据类型包括基本数据类型,引用数据类型。

注入基本数据类型

常见的基本数据类型有:short char int long float double boolean String

解决步骤

1 在配置文件中使用 property 标签 表示每一个 成员变量信息

2在目标类中 使用set 方法

demo案例

User 类

使用 Dl 注入 基本数据类型

spring 配置文件

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="fs.User" id="user" >
<!--    使用set方法注入属性-->
    <property name="name" value="李明"> </property>
    <property name="age" value="18"> </property>
    <property name="male" value="true"> </property>
</bean>
</beans>

UserTest 测试类

注入集合

常见的集合是 list ,set ,map

注入 list ,set ,map 和 基本数据类型的区别在于

在 property 标签下 都有对应的标签表示。如 list 有对应的list 标签,map 有 对应的map 标签

list,set,map

demo 案例

spring 配置文件

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="fs.User" id="user" >
<!--    使用set方法注入属性-->
    <property name="name" value="李明"> </property>
    <property name="age" value="18"> </property>
    <property name="male" value="true"> </property>
// list 
    <property name="list">
        <list>
            <value>1</value>
            <value>"黎明"</value>
            <value>ture</value>
            <value>44444</value>
        </list>
    </property>
// set 
    <property name="set">
        <set>
            <value>1</value>
            <value>"黎明"</value>
            <value>3333</value>
            <value>44444</value>
        </set>
    </property>
</bean>
</beans>

User 类

UserTest 测试类

map 集合和list,set 形式上是一样的,在对应的位置修改为 map 即可。


集合除了可以存储基本数据类型外,还可以存储 对象。我拿 list 举例

集合存储对象

demo 案例

准备工作

当前有 user ,student 类。现在要求 在 user 类有一个list 集合 把创建好的 student 对象以及自己罗列一些基本数据类型的数据存储到集合上,并打印出来。

spring 配置文件 需要修改部分

XML 复制代码
 <property name="list">
        <list>
            <value>1</value>
            <value>"黎明"</value>
            <value>ture</value>
            <value>44444</value>
          <ref bean="student"/>
        </list>
    </property>

  <bean id="student" class="fs.Student"/>

其他不变,重新运行代码

相关推荐
扶风ff35 分钟前
新品知识更新太快?用练题簿在线刷题,安排企业培训的小测与复盘
前端·学习·小程序
shuoshuohaohao37 分钟前
MyBatis 小知识点
java·spring·mybatis
海上小飞龙1 小时前
【KMP算法-下篇】同一道题:Java 库函数 2600 微秒,手写 KMP 16 微秒
java·开发语言·算法
谢亮_vipxieliang2 小时前
泛型擦除、通配符与 PECS 一次讲透
java·面试
八解毒剂2 小时前
【计组】中央处理器CPU
java·开发语言·计算机组成原理
打工仔折腾 AI3 小时前
工业场景下时序库与实时计算一体化架构选型实践对比
java·开发语言·后端·python·性能优化·架构·ai agent 实战
ZealSinger3 小时前
Java虚拟线程上线后瓶颈转移到哪看三层
java·开发语言·firefox
2601_961133283 小时前
盘立方软件指标文华期货公式下载
java
蜗牛互联网3 小时前
长任务多Agent共享文件系统的Manifest交接模式
java·人工智能·后端
传奇开心果编程3 小时前
【SwiftUI提高练中学】第13课 实时活动与灵动岛:ActivityKit 实战
学习·ui·ios·swiftui·swift