Spring - 注入集合

22 篇文章 0 订阅
14 篇文章 0 订阅

通过之前的xml配置文件中可以看到如何使用property中 value 属性以及 ref 属性配置原始的数据。以上两种case都是为bean配置单一的值

存在一种配置集合数据的场景,比如像List、Set、Map以及Properties。为了处理这种场景,Spring 提供了四种类型的集合配置元素:

NoElementDescription
1<list>帮助注入一系列的值,允许重复
2<set>帮助注入一系列的值,不允许重复
3<map>帮助注入name-value类型的集合,name和value可以是任何类型
4<props>帮助注入name-value类型的集合,name和value只能时String类型

Example

JavaCollection.java

package com.soygrow;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

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 com.soygrow;

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.getAddressMap();
        jc.getAddressSet();
        jc.getAddressProp();

        // 优雅的结束
        ((ClassPathXmlApplicationContext) context).close();
    }
}

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.xsd">

    <bean id="javaCollection" class="com.soygrow.JavaCollection">
        <property name="addressList">
            <list>
                <value>INDIA</value>
                <value>Pakistan</value>
                <value>USA</value>
                <value>USA</value>
            </list>
        </property>

        <property name="addressSet">
            <set>
                <value>INDIA</value>
                <value>Pakistan</value>
                <value>USA</value>
                <value>USA</value>
            </set>
        </property>

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

        <property name="addressProp">
            <props>
                <prop key = "one">INDIA</prop>
                <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]
Map Elements :{1=INDIA, 2=Pakistan, 3=USA, 4=USA}
Set Elements :[INDIA, Pakistan, USA]
Property Elements :{two=Pakistan, one=INDIA, three=USA, four=USA}

Injecting Bean References

以下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 Definition to handle references and values -->
   <bean id = "..." class = "...">

      <!-- Passing bean reference  for java.util.List -->
      <property name = "addressList">
         <list>
            <ref bean = "address1"/>
            <ref bean = "address2"/>
            <value>Pakistan</value>
         </list>
      </property>

      <!-- Passing bean reference  for java.util.Set -->
      <property name = "addressSet">
         <set>
            <ref bean = "address1"/>
            <ref bean = "address2"/>
            <value>Pakistan</value>
         </set>
      </property>

      <!-- Passing bean reference  for java.util.Map -->
      <property name = "addressMap">
         <map>
            <entry key = "one" value = "INDIA"/>
            <entry key = "two" value-ref = "address1"/>
            <entry key = "three" value-ref = "address2"/>
         </map>
      </property>
   </bean>

</beans>

遇到的问题

这里使用了引用<ref bean = "address1"/>但是没有这个bean。所以需要自己定义这个bean,所以需要定义address1这个类,这个类中包含你要的成员参数,然后在xml中定义这个bean。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值