spring4第三节:为集合属性赋值以及创建单例集合bean

 <!--第三节-->
 <!--为集合属性赋值,使用list,set,map-->
<!--使用list属性  -->
 <bean id="person3" class="spring.collections.Person">
     <property name="name" value="杉杉"/>
     <property name="age" value="25"/>
     <property name="car">
         <list>
             <ref bean="car"/>
             <ref bean="car2"/>
             <bean id ="car3" class="spring.beans.Car">
                 <constructor-arg value="bentian"></constructor-arg>
                 <constructor-arg value="huoche"/>
                 <constructor-arg value="30000" type="double"></constructor-arg>
             </bean>
         </list>

     </property>
 </bean>

 <!--使用map属性,以及entry进行赋值-->
 <bean id="newPerson" class="spring.collections.NewPerson">
     <property name="name" value="mapName"/>
     <property name="age" value="22"/>
     <property name="car">
         <map>
             <entry key="map1" value-ref="car"/>
             <entry key="map2" value-ref="car2"/>
         </map>
     </property>
 </bean>

 <!--使用Properties属性,一般用于用户登录,可以随意定义变量并赋值
     使用props和子节点prop为属性赋值
 -->
 <bean id="dataResource" class="spring.collections.DataResource">
     <property name="properties">
         <props>
             <prop key="username">lisi</prop>
             <prop key="password">123456</prop>
             <prop key="url">www.baidu.com</prop>
         </props>
     </property>
 </bean>

 <!--配置单例的集合bean,以供多个集合bean引用,使用util:list(减少重复代码)-->
<util:list id="cars">
    <ref bean="car"/>
    <ref bean="car2"/>
</util:list>

 <bean id="person4" class="spring.collections.Person">
     <property name="name" value="utilTestName"/>
     <property name="age" value="45"/>
     <property name="car" ref="cars"/>
 </bean>

 <!--使用p命名空间为属性赋值-->
 <bean id="person5" class="spring.collections.Person" p:name="pnamespaceName"
 p:age="50" p:car-ref="cars"/>

猜你喜欢

转载自blog.csdn.net/panruola/article/details/84996369