Spring深入浅出(六),依赖注入,注入集合(含注入 Bean 引用)

前面已经讲解如何使用 value 属性来配置基本数据类型和在 bean 配置文件中使用<property>标签的 ref 属性来配置对象引用。现在如果想传递多个值,如 Java Collection 类型 List、Set、Map 和 Properties,应该怎么做?

Spring 提供了四种类型的集合的配置元素,如下所示:

标签说明
<list> 用于注入 list 类型的值,允许重复
<set> 用于注入 set 类型的值,不允许重复
<map> 用于注入 key-value 的集合,其中 key-value 可以是任意类型
<props> 用于注入 key-value 的集合,其中 key-value 都是字符串类型

 一、创建CollectionTest类

package com.clzhang.spring.demo;

import java.util.*;

public class CollectionTest {
    String[] strings;
    List addressList;
    Set addressSet;
    Map addressMap;
    Properties addressProp;
    
    // 存放数组
    public String[] getStrings() {
        System.out.println("Array Elements :" + strings);
        return strings;
    }
    public void setStrings(String[] strings) {
        this.strings = strings;
    }
    
    // 存放List
    public void setAddressList(List addressList) {
        this.addressList = addressList;
    }
    public List getAddressList() {
        System.out.println("List Elements :" + addressList);
        return addressList;
    }

    // 存放Set
    public void setAddressSet(Set addressSet) {
        this.addressSet = addressSet;
    }
    public Set getAddressSet() {
        System.out.println("Set Elements :" + addressSet);
        return addressSet;
    }

    // 存放Map
    public void setAddressMap(Map addressMap) {
        this.addressMap = addressMap;
    }
    public Map getAddressMap() {
        System.out.println("Map Elements :" + addressMap);
        return addressMap;
    }

    // 存放Prop
    public void setAddressProp(Properties addressProp) {
        this.addressProp = addressProp;
    }
    public Properties getAddressProp() {
        System.out.println("Property Elements :" + addressProp);
        return addressProp;
    }
}

二、创建主程序

package com.clzhang.spring.demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
        CollectionTest jc = (CollectionTest) context.getBean("collectionTest");
        jc.getStrings();
        jc.getAddressList();
        jc.getAddressSet();
        jc.getAddressMap();
        jc.getAddressProp();
    }
}

三、创建配置文件

<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd">

    <!-- Definition -->
    <bean id="address1" class="com.clzhang.spring.demo.Address1" />
    <bean id="collectionTest"
        class="com.clzhang.spring.demo.CollectionTest">
        <property name="strings">
            <list>
                <value>印度</value>
                <value>日本</value>
                <value>中国</value>
                <value>中国</value>
            </list>
        </property>

        <property name="addressList">
            <list>
                <value>印度</value>
                <value>日本</value>
                <value>中国</value>
                <value>中国</value>
                <ref bean="address1" />
            </list>
        </property>

        <property name="addressSet">
            <set>
                <value>印度</value>
                <value>日本</value>
                <value>中国</value>
                <value>中国</value>
                <ref bean="address1" />
            </set>
        </property>

        <property name="addressMap">
            <map>
                <entry key="1" value="印度" />
                <entry key="2" value="日本" />
                <entry key="3" value="中国" />
                <entry key="4" value="中国" />
                <entry key="5" value-ref="address1" />
            </map>
        </property>

        <property name="addressProp">
            <props>
                <prop key="one">印度</prop>
                <prop key="two">日本</prop>
                <prop key="three">中国</prop>
                <prop key="four">中国</prop>
            </props>
        </property>
    </bean>
</beans>

四、创建注入Bean

package com.clzhang.spring.demo;

public class Address1 {
    public String toString() {
        return "地址一";
    }
}

五、运行

Array Elements :[Ljava.lang.String;@45820e51
List Elements :[印度, 日本, 中国, 中国, 地址一]
Set Elements :[印度, 日本, 中国, 地址一]
Map Elements :{1=印度, 2=日本, 3=中国, 4=中国, 5=地址一}
Property Elements :{two=日本, one=印度, three=中国, four=中国}

本文参考:

https://www.w3cschool.cn/wkspring/kp5i1ico.html

http://c.biancheng.net/spring/inject-collection.html

原文地址:https://www.cnblogs.com/nayitian/p/15001672.html