Spring 基于XML的集合注入

Spring 基于XML的集合注入

现在如果想传递多个值,如 Java Collection 类型 List、Set、Map 和 Properties,应该怎么做呢。为了处理这种情况,Spring 提供了四种类型的集合的配置元素,如下所示:
在这里插入图片描述

  • 配置
<beans xmlns="http://www.springframework.org/schema/beans"
       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">

    <!--
         集合的注入都是给<property>添加子标签
             数组:<array>
             List:<list>
             Set:<set>
             Map:<map> ,map存放k/v 键值对,使用<entry>描述
             Properties:<props>  <prop key=""></prop>

         普通数据:<value>
         引用数据:<ref>
     -->
    <bean id="collDataId" class="com.cc.study.di.CollData" >
        <property name="arrayData">
            <array>
                <value>A</value>
                <value>B</value>
                <value>C</value>
                <value>D</value>
                <!-- <ref bean="xxx"/>  //bean 的引用作为集合的元素 -->
            </array>
        </property>

        <property name="listData">
            <list>
                <value>A</value>
                <value>B</value>
                <value>C</value>
                <value>D</value>
            </list>
        </property>

        <property name="setData">
            <set>
                <value>A</value>
                <value>B</value>
                <value>C</value>
            </set>
        </property>

        <property name="mapData">
            <map>
                <entry key="A" value="AA"></entry>
                <!-- <entry key ="two" value-ref="xxx"/> //bean 的引用作为map的元素-->
                <entry>
                    <key><value>B</value></key>
                    <value>BB</value>
                </entry>
            </map>
        </property>

        <property name="propsData">
            <props>
                <prop key="A">AA</prop>
                <prop key="B">BB</prop>
                <prop key="C">CC</prop>
            </props>
        </property>
        <!-- 如果你需要传递一个 NULL 值,那么你可以传递<null/>-->
        <property name="str"><null/></property>
    </bean>

</beans>

  • CollData
public class CollData {

    private String[] arrayData;
    private List<String> listData;
    private Set<String> setData;
    private Map<String,String> mapData;
    private Properties propsData;
    private String str = "123";

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public String[] getArrayData() {
        return arrayData;
    }

    public void setArrayData(String[] arrayData) {
        this.arrayData = arrayData;
    }

    public List<String> getListData() {
        return listData;
    }

    public void setListData(List<String> listData) {
        this.listData = listData;
    }

    public Set<String> getSetData() {
        return setData;
    }

    public void setSetData(Set<String> setData) {
        this.setData = setData;
    }

    public Map<String, String> getMapData() {
        return mapData;
    }

    public void setMapData(Map<String, String> mapData) {
        this.mapData = mapData;
    }

    public Properties getPropsData() {
        return propsData;
    }

    public void setPropsData(Properties propsData) {
        this.propsData = propsData;
    }
}

  • 测试
@Test
    public void demo12(){
        String xmlPath = "di7.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        CollData c = applicationContext.getBean("collDataId",CollData.class);
        System.out.println(Arrays.asList(c.getArrayData()));
        System.out.println(c.getListData());
        System.out.println(c.getSetData());
        System.out.println(c.getMapData());
        System.out.println(c.getPropsData());
        System.out.println(c.getStr());
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值