Spring 注入集合

如果你想传递多个值,如 Java Collection 类型 List、Set、Map 和 Properties,应该怎么做呢。

为了处理这种情况,Spring 提供了四种类型的集合的配置元素,如下所示:

一个示例

  • 新建一个Spring项目

  • 新建Java文件:JavaCollection.java和MainApp.java

这里是 JavaCollection.java 文件的内容:

package hello;
import java.util.*;

public class JavaCollection {
    List addressList;
    Set addressSet;
    Map addressMap;
    Properties addressProp;
    // a setter method to set List
    public void setAddressList(List addressList){
        this.addressList = addressList;
    }
    // prints and returns all the elements of the list.
    public List getAddressList(){
        System.out.println("List Elements: "+ addressList);
        return addressList;
    }
    // a setter method to set Set
    public void setAddressSet(Set addressSet){
        this.addressSet = addressSet;
    }
    // prints and returns all the elements of the Set.
    public Set getAddressSet(){
        System.out.println("Set Elements: "+addressSet);
        return addressSet;
    }
    // a setter method to set Map
    public void setAddressMap(Map addressMap){
        this.addressMap = addressMap;
    }
    // prints and returns all the elements of the Map.
    public Map getAddressMap(){
        System.out.println("Map Elements: "+addressMap);
        return addressMap;
    }
    // a setter method to set Property
    public void setAddressProp(Properties addressProp){
        this.addressProp = addressProp;
    }
    // prints and returns all the elements of the Property.
    public Properties getAddressProp(){
        System.out.println("Property Elements: "+ addressProp);
        return addressProp;
    }
}

下面是 MainApp.java 文件的内容:

package hello;
//import org.springframework.context.support.AbstractApplicationContext;
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.getAddressList();
         jc.getAddressSet();
         jc.getAddressMap();
         jc.getAddressProp();
    }
}

下面是配置所有类型的集合的配置文件 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" xmlns:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">

    <!-- Definition for javaCollection bean-->
    <bean id="javaCollection" class="hello.JavaCollection" >

        <!-- results in a setAddressList(java.util.List) call -->
        <property name="addressList">
                <util:list>
                    <value>INDIA</value>
                    <value>Pakistan</value>
                    <value>USA</value>
                    <value>USA</value>
                </util:list>
        </property>

        <!-- results in a setAddressSet(java.util.Set) call -->
        <property name="addressSet">
            <set>
                <value>INDIA</value>
                <value>Pakistan</value>
                <value>USA</value>
                <value>USA</value>
            </set>
        </property>

        <!-- results in a setAddressMap(java.util.Map) call -->
        <property name="addressMap">
            <map>
                <entry key="1" value="INDIA"/>
                <entry key="2" value="Pakistan"/>
                <entry key="3" value="USA"/>
                <entry key="4" value="USA"/>
            </map>
        </property>

        <!-- results in a setAddressProp(java.util.Properties) call -->
        <property name="addressProp">
            <props>
                <prop key="one">INDIA</prop>
                <prop key="two">Pakistan</prop>
                <prop key="three">USA</prop>
                <prop key="four">USA</prop>
            </props>
        </property>

    </bean>

</beans>

运行结果如下:

List Elements: [INDIA, Pakistan, USA, USA]
Set Elements: [INDIA, Pakistan, USA]
Map Elements: {1=INDIA, 2=Pakistan, 3=USA, 4=USA}
Property Elements: {two=Pakistan, one=INDIA, three=USA, four=USA}

每天学习一点点,每天进步一点点。

原文地址:https://www.cnblogs.com/youcoding/p/12756012.html