Spring - 注入集合
您已经了解了如何使用 Bean 配置文件中的 <property> 标签的 value 属性和对象引用来配置原始数据类型和对象引用。 这两种情况都处理将单数值传递给 bean。
现在,如果您想传递多个值,例如 Java 集合类型,如 List、Set、Map 和 Properties,该怎么办。 为了处理这种情况,Spring 提供了四种类型的集合配置元素,如下所示 −
| 序号 | 元素 & 描述 |
|---|---|
| 1 |
<list> This helps in wiring ie injecting a list of values, allowing duplicates. |
| 2 |
<set> 这有助于连接一组值,但没有任何重复。 |
| 3 |
<map> 这可用于注入名称-值对的集合,其中名称和值可以是任何类型。 |
| 4 |
<props> 这可用于注入名称和值都是字符串的名称-值对集合。 |
您可以使用 <list> 或 <set> 来连接 java.util.Collection 的任何实现或 array。
您将遇到两种情况(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;
}
// 设置属性的 setter 方法
public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
}
// 打印并返回属性的所有元素。
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">
<!-- results in a setAddressList(java.util.List) call -->
<property name = "addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</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 = "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]
ap 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)

