1.集合为普通属性
<bean id="stu" class="com.company.spring5.collectionType.Stu"> <!--数组类型的注入--> <property name="course"> <array> <value>java课程</value> <value>数据库课程</value> <value>jsp</value> </array> </property> <property name="list"> <list> <value>zk</value> <value>zkk</value> <value>zkkk</value> </list> </property> <property name="map"> <map> <entry key="1" value="zk"></entry> <entry key="2" value="zkk"></entry> <entry key="3" value="zkkk"></entry> </map> </property> <property name="set"> <set> <value>吗</value> <value>吗吗</value> <value>吗吗吗</value> </set> </property> </bean>
2.集合为对象属性
外部bean注入法
<property name="courses"> <list> <ref bean="course"></ref> <ref bean="course2"></ref> </list> </property>
<bean id="course" class="com.company.spring5.collectionType.Course"> <property name="cname"> <value>Spring5</value> </property> </bean> <bean id="course2" class="com.company.spring5.collectionType.Course"> <property name="cname"> <value>mybats</value> </property> </bean>
3.集合部分抽取出来注入
1.引入命名空间util
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" 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 http://www.springframework.org/schema/util http://www.springfra
2.使用util标签实现集合的抽取
<!--集合类型的提取注入--> <util:list id="bookList"> <value>a</value> <value>b</value> <value>c</value> </util:list>
3.抽取集合的注入 通过ref注入
<bean id="book" class="com.company.spring5.collectionType.Book"> <property name="list" ref="bookList"></property> </bean>