qidao123.com技术社区-IT企服评测·应用市场
标题:
spring 学习(spring-Dl补充(注入差别类型的数据))
[打印本页]
作者:
饭宝
时间:
2025-2-12 13:37
标题:
spring 学习(spring-Dl补充(注入差别类型的数据))
媒介
在之前的案例,枚举的最多的是注入 对象。本篇博客则是补充说我们不但可以注入对象 还可以注入其他的数据类型包括根本数据类型,引用数据类型。
注入根本数据类型
常见的根本数据类型有:short char int long float double boolean String
解决步骤
1 在配置文件中利用 property 标签 体现每一个 成员变量信息
2在目标类中 利用set 方法
demo案例
User 类
利用 Dl 注入 根本数据类型
spring 配置文件
<?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 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 配置文件 必要修改部分
<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"/>
复制代码
其他不变,重新运行代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/)
Powered by Discuz! X3.4