Spring -> 依赖配置:集合

idref
参考https://blog.csdn.net/soonfly/article/details/68928469
更改idref后报错,请参考其他文章
内部对象
https://blog.csdn.net/soonfly/article/details/68928511
复合属性
https://blog.csdn.net/soonfly/article/details/68928534

Spring中通过<list>,<set>,<map>,<props>元素,来设置集合类型的属性或参数。
它们分别对应java类型的List,Set,Map,Properties

<bean id="ordercollect" class="com.zl.spring.OrderCollect">
    <!-- property对象的设置 -->
    <property name="Emailconfig">
        <props>
            <prop key="pop">pop.twm.com</prop>
            <prop key="user">[email protected]</prop>
            <prop key="pwd">123456</prop>
        </props>
    </property>

    <!-- list对象的设置,数组的设置方式同这个一样 -->
    <property name="Productnames">
        <list>
            <value>绿茶</value>
            <value>小番茄</value>
            <!-- 外部对象就用ref指向 bean id 
            <ref bean="mybeanid" /> -->
        </list>
    </property>

    <!-- map对象的设置 -->
    <property name="purchaseList">
        <map>
            <entry key="SN-1137569" value="3个"/>
            <entry key="SN-1137571" value="1个"/>
            <!-- 外部对象就用value-ref指向 bean id 
            <entry key ="arefkey" value-ref="mybeanid"/>-->
        </map>
    </property>

    <!-- set对象的设置 -->
    <property name="setOthers">
       <set>
           <value>要小票</value>
           <value>放整齐包严实</value>
           <!-- 外部对象就用ref指向 bean id 
           <ref bean="mybeanid" />-->
       </set>
   </property>

</bean>

输出

OrderCollect [Emailconfig={pop=pop.twm.com, [email protected], pwd=123456}, Productnames=[绿茶, 小番茄], purchaseList={SN-1137569=3个, SN-1137571=1个}, setOthers=[要小票, 放整齐包严实]]

所有集合的value值可以是普通值类型,也可以是对象或集合:
bean | ref | idref | list | set | map | props | value | null

集合合并

Spring容器也支持集合合并。合并的场景主要出现在父子集合。
也就是,子集合中的值是合并父子集合后的值,其中子集合中的值会覆盖父集合中的值。

Spring Bean的父子继承主要是为了统一定义Spring Bean的公共属性、作业范围scope,并避免了冗余和修改的繁琐

<beans>
<bean id="notify" class="com.zl.spring.NotifyServiceByCellPhoneImpl" />
	<!-- 父对象声明了属性abstract="true",则不能实例化,因为它是抽象的bean。
	<!-- 容器内部的preInstantiateSingletons()方法会忽略抽象bean。在作为子bean的纯模板时,非常有用 -->
    <bean id="parent" abstract="true" class="example.ComplexObject">
      <property name="notifyservice" ref="notify"/>
        <property name="adminEmails">
            <props>
                <prop key="administrator">[email protected]</prop>
                <prop key="support">[email protected]</prop>
            </props>
        </property>
    </bean>
	<!-- child bean无需声明class,它用parent声明了父对象,这样就继承了父对象的class声明,-->
	<!-- 以及notifyservice属性值,adminEmails的值也会从父对象继承并合并 -->
    <bean id="child" parent="parent">
        <property name="adminEmails">
        	<!-- merge="true"表示开启合并 -->
            <props merge="true">
                <prop key="sales">[email protected]</prop>
                <prop key="support">[email protected]</prop>
            </props>
        </property>
    </bean>
<beans>

child的值:
[email protected]
[email protected]
[email protected]

这个合并行为,同样可以用在<list>,<map>,<set>类型上

集合合并总结:
1、list没有覆盖行为。
2、对于元素,spring合并行为维持集合原有的顺序序;父集合中的元素索引位置比子集合元素索引位置靠前。
3、不能合并不同类型的集合,比如合并Map和List。
4、merge属性必须指定给父-子继承结构中的子bean,如果指定给了父bean则不会产生预期的合并结果。

发布了25 篇原创文章 · 获赞 0 · 访问量 337

猜你喜欢

转载自blog.csdn.net/weixin_45808666/article/details/103834969