Spring注入集合

如果需要传递类似于 Java Collection 类型的值,例如 List、Set、Map 和 properties,可以使用 Spring 提供的集合配置标签,如下表所示。

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

示例

下面使用 Eclipse IDE 演示如何注入集合,步骤如下:

  1. 创建 SpringDemo 项目,并在 src 目录下创建 net.biancheng 包。
  2. 添加相应的 jar 包。
  3. 在 net.biancheng 包下创建 JavaCollection、Man 和 MainApp 类。
  4. 在 src 目录下创建 Spring 配置文件 Beans.xml。
  5. 运行 SpringDemo 项目。


JavaCollection 类代码如下。

package net.biancheng;

import java.util.*;

public class JavaCollection {
    List manList;
    Set manSet;
    Map manMap;
    Properties manProp;

    public void setManList(List manList) {
        this.manList = manList;
    }

    public List getManList() {
        System.out.println("List Elements :" + manList);
        return manList;
    }

    public void setManSet(Set manSet) {
        this.manSet = manSet;
    }

    public Set getManSet() {
        System.out.println("Set Elements :" + manSet);
        return manSet;
    }

    public void setManMap(Map manMap) {
        this.manMap = manMap;
    }

    public Map getManMap() {
        System.out.println("Map Elements :" + manMap);
        return manMap;
    }

    public void setManProp(Properties manProp) {
        this.manProp = manProp;
    }

    public Properties getManProp() {
        System.out.println("Property Elements :" + manProp);
        return manProp;
    }
}

MainApp 类代码如下。

package net.biancheng;

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");
        JavaCollection jc = (JavaCollection) context.getBean("javaCollection");

        jc.getManList();
        jc.getManSet();
        jc.getManMap();
        jc.getManProp();
    }
}

Beans.xml 配置文件如下。

<?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">

    <bean id="javaCollection" class="net.biancheng.JavaCollection">

        <property name="manList">
            <list>
                <value>编程帮</value>
                <value>百度</value>
                <value>C语言中文网</value>
                <value>C语言中文网</value>
            </list>
        </property>

        <property name="manSet">
            <set>
                <value>编程帮</value>
                <value>百度</value>
                <value>C语言中文网</value>
                <value>C语言中文网</value>
            </set>
        </property>

        <property name="manMap">
            <map>
                <entry key="1" value="编程帮" />
                <entry key="2" value="百度" />
                <entry key="3" value="C语言中文网" />
                <entry key="4" value="C语言中文网" />
            </map>
        </property>

        <property name="manProp">
            <props>
                <prop key="one">编程帮</prop>
                <prop key="one">编程帮</prop>
                <prop key="two">百度</prop>
                <prop key="three">C语言中文网</prop>
                <prop key="four">C语言中文网</prop>
            </props>
        </property>
    </bean>

</beans>


运行结果如下。

List Elements :[编程帮, 百度, C语言中文网, C语言中文网]
Set Elements :[编程帮, 百度, C语言中文网]
Map Elements :{1=编程帮, 2=百度, 3=C语言中文网, 4=C语言中文网}
Property Elements :{two=百度, one=编程帮, three=C语言中文网, four=C语言中文网}

注入Bean引用

也可以在集合元素中注入 Bean,如下所示。

<?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">

    <bean id="..." class="...">

        <property name="manList">
            <list>
                <ref bean="man1" />
                <ref bean="man2" />
                <value>编程帮</value>
            </list>
        </property>

        <property name="manSet">
            <set>
                <ref bean="man1" />
                <ref bean="man2" />
                <value>编程帮</value>
            </set>
        </property>

        <property name="manMap">
            <map>
                <entry key="one" value="编程帮" />
                <entry key="two" value-ref="man1" />
                <entry key="three" value-ref="man2" />
            </map>
        </property>
    </bean>
</beans>

注入null和空字符串的值

Spring 会把属性的空参数直接当成空字符串来处理,如果您需要传递一个空字符串值,可以这样写:

  • <bean id = "..." class = "exampleBean">
  • <property name = "email" value = ""/>
  • </bean>

等效于以下代码

  • exampleBean.setEmail("")


如果需要传递 NULL 值,<null/> 元素用来处理 Null 值。

  • <bean id = "..." class = "exampleBean">
  • <property name = "email"><null/></property>
  • </bean>

等效于以下代码

    • exampleBean.setEmail(null)

来源:

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

原文地址:https://www.cnblogs.com/emanlee/p/15146243.html