应用<property>标签的value属性配置原始数据类型和ref属性配置对象援用的形式来定义Bean配置文件。这两种状况都波及将繁多值传递给Bean。那么如果您想传递多个值,例如Java汇合类型,如List、Set、Map和Properties怎么办?为了解决这种状况,Spring提供了四种类型的汇合配置元素,如下所示:

序号元素 & 形容
1<list>
用于注入一组值,容许反复。
2<set>
用于注入一组值,但不容许反复。
3<map>
可用于注入一组名称-值对,其中名称和值能够是任何类型。
4<props>
可用于注入一组名称-值对,其中名称和值都是字符串。

您能够应用<list><set>来注入java.util.Collection的任何实现或数组。

在解决汇合时,通常会遇到两种状况:(a)传递汇合的间接值和(b)将Bean的援用作为汇合元素之一传递。

示例

假如您曾经筹备好Eclipse IDE,并采取以下步骤创立Spring应用程序:

步骤 形容

1 创立一个名为SpringExample的我的项目,在创立的我的项目中的src文件夹下创立一个名为com.tutorialspoint的包。

2 应用"Add External JARs"选项增加所需的Spring库,如Spring Hello World示例章节中所述。

3 在com.tutorialspoint包下创立Java类JavaCollection和MainApp。

4 在src文件夹下创立Beans配置文件Beans.xml。

5 最初一步是创立所有Java文件和Bean配置文件的内容,并按以下阐明运行应用程序。

以下是JavaCollection.java文件的内容:

package com.tutorialspoint;import java.util.*;public class JavaCollection {   List addressList;   Set  addressSet;   Map  addressMap;   Properties addressProp;   // 用于设置List的setter办法   public void setAddressList(List addressList) {      this.addressList = addressList;   }      // 打印并返回列表的所有元素。   public List getAddressList() {      System.out.println("List Elements :"  + addressList);      return addressList;   }      // 用于设置Set的setter办法   public void setAddressSet(Set addressSet) {      this.addressSet = addressSet;   }      // 打印并返回Set的所有元素。   public Set getAddressSet() {      System.out.println("Set Elements :"  + addressSet);      return addressSet;   }      // 用于设置Map的setter办法   public void setAddressMap(Map addressMap) {      this.addressMap = addressMap;   }      // 打印并返回Map的所有元素。   public Map getAddressMap() {      System.out.println("Map Elements :"  + addressMap);      return addressMap;   }      // 用于设置Property的setter办法   public void setAddressProp(Properties addressProp) {      this.addressProp = addressProp;   }      // 打印并返回Property的所有元素。   public Properties getAddressProp() {      System.out.println("Property Elements :"  + addressProp);      return addressProp;   }}

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

package com.tutorialspoint;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"   xsi:schemaLocation = "http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">   <!-- Definition for javaCollection -->   <bean id = "javaCollection" class = "com.tutorialspoint.JavaCollection">            <!-- 产生 setAddressList(java.util.List) 调用 -->      <property name = "addressList">         <list>            <value>INDIA</value>            <value>Pakistan</value>            <value>USA</value>            <value>USA</value>         </list>      </property>      <!-- 产生 setAddressSet(java.util.Set) 调用 -->      <property name = "addressSet">         <set>            <value>INDIA</value>            <value>Pakistan</value>            <value>USA</value>            <value>USA</value>         </set>      </property>      <!-- 产生 setAddressMap(java.util.Map) 调用 -->      <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>            <!-- 产生 setAddressProp(java.util.Properties) 调用 -->      <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>

当您实现创立源代码和Bean配置文件后,让咱们运行应用程序。如果一切正常,应用程序将打印以下音讯:

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}

注入Bean援用

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

要应用上述Bean定义,您须要以使它们可能解决援用的形式定义setter办法。

注入null和空字符串值

如果须要传递空字符串作为值,能够应用以下形式传递:

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

上述示例等效于Java代码:exampleBean.setEmail("")

如果须要传递NULL值,能够应用以下形式传递:

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

上述示例等效于Java代码:exampleBean.setEmail(null)

最初

为了不便其余设施和平台的小伙伴观看往期文章,链接奉上:

公众号搜寻Let us Coding知乎开源中国CSDN思否掘金InfoQ简书博客园慕课51CTOhelloworld腾讯开发者社区阿里开发者社区

看完如果感觉有帮忙,欢送点赞、珍藏关注