依赖注入的几种方式
为数组类型属性赋值
直接配置property中的array属性
<bean id="studentFive" class="com.gu.spring.pojo.Student"> <property name="sid" value="1003"></property> <property name="sname" value="lisi"></property> <property name="age" value="23"></property> <property name="gender" value="男"></property> <property name="hobby"> <array> <value>抽烟</value> <value>喝酒</value> <value>烫头</value> </array> </property> </bean>
为集合属性赋值
为list属性赋值
<bean id="clazzOne" class="com.gu.spring.pojo.Clazz"> <property name="cid" value="1111"></property> <property name="cname" value="最强王者班"></property> <property name="students" ref="studentList"></property> <property name="students"> <list> <ref bean="studentOne"></ref> <ref bean="studentTwo"></ref> <ref bean="studentThree"></ref> </list> </property> </bean>
若为Set集合类型属性赋值,只需要将其中的list标签改为set标签即可
为map属性赋值
<property name="teacherMap" ref="teacherMap"></property> <property name="teacherMap"> <map> <entry key="10086" value-ref="teacherOne"></entry> <entry key="10087" value-ref="teacherTwo"></entry> </map> </property>
引用集合类型的bean
<!-- 配置一个集合类型的bean需要使用util的约束 --> <util:list id="studentList"> <ref bean="studentOne"></ref> <ref bean="studentTwo"></ref> <ref bean="studentThree"></ref> </util:list> <bean id="teacherOne" class="com.gu.spring.pojo.Teacher"> <property name="tid" value="10086"></property> <property name="tname" value="yiyi"></property> </bean> <bean id="teacherTwo" class="com.gu.spring.pojo.Teacher"> <property name="tid" value="10087"></property> <property name="tname" value="erer"></property> </bean> <util:map id="teacherMap"> <entry key="10086" value-ref="teacherOne"></entry> <entry key="10087" value-ref="teacherTwo"></entry> </util:map> <bean id="studentSix" class="com.gu.spring.pojo.Student" p:sid="1005" p:sname="xiaoming" p:gender="女" p:teacherMap-ref="teacherMap" ></bean>
over
标签:依赖,list,day121,几种,集合,属性,赋值 From: https://www.cnblogs.com/GUGUZIZI/p/17606492.html