( 九 ) Spring 注入集合

( 九 ) Spring 注入集合

 

1、简介

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

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

2、示例

JavaCollection 类代码如下:

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 类代码如下:

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="com.dw.study.JavaCollection">
        <property name="manList">
            <list>
                <value>test01</value>
                <value>test02</value>
                <value>test03</value>
                <value>test04</value>
            </list>
        </property>
        <property name="manSet">
            <set>
                <value>test01</value>
                <value>test02</value>
                <value>test03</value>
                <value>test04</value>
            </set>
        </property>
        <property name="manMap">
            <map>
                <entry key="1" value="test01" />
                <entry key="2" value="test02" />
                <entry key="3" value="test03" />
                <entry key="4" value="test04" />
            </map>
        </property>
        <property name="manProp">
            <props>
                <prop key="one">test01</prop>
                <prop key="one">test04</prop>
                <prop key="two">test05</prop>
                <prop key="three">test06</prop>
                <prop key="four">test07</prop>
            </props>
        </property>
    </bean>
</beans>

 

注入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>test003</value>
            </list>
        </property>
        <property name="manSet">
            <set>
                <ref bean="man1" />
                <ref bean="man2" />
                <value>test004</value>
            </set>
        </property>
        <property name="manMap">
            <map>
                <entry key="one" value="test005" />
                <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>

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

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

 

posted @ 2021-07-28 14:08  邓维-java  阅读(409)  评论(0)    收藏  举报