3.IOC 操作 Bean 管理-xml 注入集合属性

4.IOC 操作 Bean 管理(xml 注入集合属性)

1、注入数组类型属性、注入 List 集合类型属性、注入 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.stt.pojo.Address"/>
<bean id="student" class="com.stt.pojo.Student">
<property name="name" value="leizia"/>
<property name="address" ref="address"/>
<!--数组-->
<property name="books">
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>三国演义</value>
<value>水浒传</value>
</array>
</property>
<!--list-->
<property name="hobbys">
<list>
<value>学习</value>
<value>java</value>
</list>
</property>
<!--map-->
<property name="card">
<map>
<entry key="手机号" value="1101"/>
</map>
</property>
<!--set-->
<property name="games">
<set>
<value>LOL</value>
<value>CS</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">1001</prop>
<prop key="成绩">100</prop>
</props>
</property>
</bean>
</beans>

2、在集合里面设置对象类型值

<!--创建多个 course 对象-->
<bean id="course1" class="com.leizi.spring5.collectiontype.Course">
<property name="cname" value="Spring5 框架"></property>
</bean>
<bean id="course2" class="com.leizi.spring5.collectiontype.Course">
<property name="cname" value="MyBatis 框架"></property>
</bean>
<!--注入 list 集合类型,值是对象-->
<property name="courseList">
<list>
<ref bean="course1"></ref>
<ref bean="course2"></ref>
</list>
</property>

3、把集合注入部分提取出来

(1)在 spring 配置文件中引入名称空间 util

(2)使用 util 标签完成 list 集合注入提取

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
<!--spring 配置文件中引入名称空间 util-->
xmlns:util="http://www.springframework.org/schema/util
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!--1 提取 list 集合类型属性注入-->
<util:list id="bookList"><value>易筋经</value>
<value>九阴真经</value>
<value>九阳神功</value>
</util:list>
<!--2 提取 list 集合类型属性注入使用-->
<bean id="book" class="com.atguigu.spring5.collectiontype.Book">
<property name="list" ref="bookList"></property>
</bean>
posted @   Lz_蚂蚱  阅读(48)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起