Spring Dependency Injection 简明教程
Spring DI - Injecting Collections Constructor
您已经看到如何使用 Bean 配置文件中的 <property> 标记的 value 属性配置原始数据类型,以及使用 ref 属性配置对象引用。这两种情况都涉及将奇异值传递给 Bean。
现在,如果您想传递像 Java Collection 类型(比如 List、Set 和 Properties)这样的复数值该怎么办?为了应对这种情况,Spring 提供了以下类型的集合配置元素,如下所示 −
Sr.No |
Element & Description |
1 |
<list> 这有助于进行接线,即注入一个值列表,同时允许重复。 |
2 |
<set> 这有助于接线一组值,但没有任何重复。 |
3 |
<props> 这可用于注入名称-值对的集合,其中名称和值都是字符串。 |
您可以使用 <list> 或 <set> 来接线任何 java.util.Collection 的实现或 array 。
在该示例中,我们展示的是传递集合元素的直接值。
Example
下面的示例展示了 JavaCollection 类,它使用集合作为依赖项,这些集合使用构造函数参数进行注入。
让我们更新在 Spring DI - Create Project 章节中创建的项目。我们将添加以下文件 −
-
JavaCollection.java − 包含一个集合作为依赖项的类。
-
MainApp.java - 要运行和测试的主应用。
以下是 JavaCollection.java 文件的内容−
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List<String> addressList;
Set<String> addressSet;
Properties addressProp;
public JavaCollection() {}
public JavaCollection(List<String> addressList, Set<String> addressSet,
Properties addressProp) {
this.addressList = addressList;
this.addressSet = addressSet;
this.addressProp = addressProp;
}
// a setter method to set List
public void setAddressList(List<String> addressList) {
this.addressList = addressList;
}
// prints and returns all the elements of the list.
public List<String> getAddressList() {
System.out.println("List Elements :" + addressList);
return addressList;
}
// a setter method to set Set
public void setAddressSet(Set<String> addressSet) {
this.addressSet = addressSet;
}
// prints and returns all the elements of the Set.
public Set<String> getAddressSet() {
System.out.println("Set Elements :" + addressSet);
return addressSet;
}
// 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.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("applicationcontext.xml");
JavaCollection jc=(JavaCollection)context.getBean("javaCollection");
jc.getAddressList();
jc.getAddressSet();
jc.getAddressProp();
}
}
以下是配置文件 applicationcontext.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.tutorialspoint.JavaCollection">
<constructor-arg name = "addressList">
<list>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</list>
</constructor-arg>
<constructor-arg name = "addressSet">
<set>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</set>
</constructor-arg>
<constructor-arg name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "two">JAPAN</prop>
<prop key = "three">USA</prop>
<prop key = "four">UK</prop>
</props>
</constructor-arg>
</bean>
</beans>
完成源文件和 Bean 配置文件创建后,我们运行该应用程序。如果您的应用程序一切正常,它将打印以下消息−
List Elements :[INDIA, JAPAN, USA, UK]
Set Elements :[INDIA, JAPAN, USA, UK]
Property Elements :{four=UK, one=INDIA, two=JAPAN, three=USA}