集合已经集合的合并(多个集合的值合并到一起)


集合

通过<list/>、<set/>、<map/>及<props/>元素可以定义和设置与Java Collection类型对应List、Set、Map及Properties的值。

<bean id="moreComplexObject" class="example.ComplexObject">
  <!-- results in a setAdminEmails(java.util.Properties) call -->
  <property name="adminEmails">
    <props>
        <prop key="administrator">[email protected]</prop>
        <prop key="support">[email protected]</prop>
        <prop key="development">[email protected]</prop>
    </props>
  </property>
  <!-- results in a setSomeList(java.util.List) call -->
  <property name="someList">
    <list>
        <value>a list element followed by a reference</value>
        <ref bean="myDataSource" />
    </list>
  </property>
  <!-- results in a setSomeMap(java.util.Map) call -->
  <property name="someMap">
    <map>
        <entry>
            <key>
                <value>an entry</value>
            </key>
            <value>just some string</value>
        </entry>
        <entry>
            <key>
                <value>a ref</value>
            </key>
            <ref bean="myDataSource" />
        </entry>
    </map>
  </property>
  <!-- results in a setSomeSet(java.util.Set) call -->
  <property name="someSet">
    <set>
        <value>just some string</value>
        <ref bean="myDataSource" />
    </set>
  </property>
</bean>
注意:map的key或value值,或set的value值还可以是以下元素:

bean | ref | idref | list | set | map | props | value | null

集合的合并
从2.0开始,Spring IoC容器将支持集合的合并。这样我们可以定义parent-style和child-style的<list/>、<map/>、<set/>或<props/>元素,子集合的值从其父集合继承和覆盖而来;也就是说,父子集合元素合并后的值就是子集合中的最终结果,而且子集合中的元素值将覆盖父集全中对应的值。

<bean id="account" class="com.liyixing.spring.model.Account"
parent="parent">
<property name="p">
<props merge="true">
<prop key="support">my</prop>
</props>
</property>
</bean>
<bean id="parent" abstract="true" class="com.liyixing.spring.model.Account">
<property name="p">
<props>
<prop key="a">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
</bean>
这里的区别在 <props merge="true"> merge=true这个表示合并。相同的key将会被覆盖

另外我们也可以把它拆分成多个文件
在bean.xml中定义
<bean id="account" class="com.liyixing.spring.model.Account"
parent="parent">
<property name="p">
<props merge="true">
<prop key="support">my</prop>
</props>
</property>
</bean>
在bean1.xml定义

<bean id="parent" abstract="true" class="com.liyixing.spring.model.Account">
<property name="p">
<props>
<prop key="a">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>
</bean>
依然是一样的。

list,我这里把parent和account定义在两个文件

package com.liyixing.spring.model;

import java.util.List;
import java.util.Properties;

public class Account {
private Properties p;
private List<String> l;

public List<String> getL() {
return l;
}

public void setL(List<String> l) {
this.l = l;
}

public Properties getP() {
return p;
}

public void setP(Properties p) {
this.p = p;
}
}
bean1.xml
<bean id="parent" abstract="true" class="com.liyixing.spring.model.Account">
<property name="p">
<props>
<prop key="a">[email protected]</prop>
<prop key="support">[email protected]</prop>
</props>
</property>

<property name="l">
<list>
<value>1iyixing</value>
<value>1iyimao</value>
</list>
</property>
</bean>
</beans>
bean.xml

<bean id="account" class="com.liyixing.spring.model.Account"
parent="parent">
<property name="p">
<props merge="true">
<prop key="support">my</prop>
</props>
</property>

<property name="l">
<list merge="true">
<value>test</value>
</list>
</property>
</bean>

测试,可以看到


两个list合并到了一起。
这个功能我以前一直想找找怎么做,可以让DataSource的映射列表定义成多个文件。一直没找到,今天看到了spring的开发手册的这个功能,应该能实现了。

强类型集合(仅适用于Java5+)

你若有幸在使用Java5 或Java 6,那么你可以使用强类型集合(支持泛型)。比如,声明一个只能包含String类型元素的Collection。假若使用Spring来给bean注入强类型的Collection,那就可以利用Spring的类型转换能,当向强类型Collection中添加元素前,这些元素将被转换。

public class Foo {
               
    private Map<String, Float> accounts;
   
    public void setAccounts(Map<String, Float> accounts) {
        this.accounts = accounts;
    }
}
<beans>
    <bean id="foo" class="x.y.Foo">
        <property name="accounts">
            <map>
                <entry key="one" value="9.99"/>
                <entry key="two" value="2.75"/>
                <entry key="six" value="3.99"/>
            </map>
        </property>
    </bean>
</beans>
在foobean的accounts属性被注入之前,通过反射,利用强类型Map<String, Float>的泛型信息,Spring的底层类型转换机制将会把各种value元素值转换为Float类型,因此字符串9.99、2.75及3.99就会被转换为实际的Float类型。

猜你喜欢

转载自liyixing1.iteye.com/blog/1036564