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>
本文来自博客园,作者:Lz_蚂蚱,转载请注明原文链接:https://www.cnblogs.com/leizia/p/14959256.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步