- Spring DI 教程
- Spring DI - 主页
- Spring DI - 概述
- Spring DI - 环境设置
- Spring DI - IOC 容器
- Spring依赖注入
- Spring DI - 创建项目
- 基于构造函数的注入示例
- Spring DI - 基于构造函数
- Spring DI - 内部 Bean 构造函数
- Spring DI - 集合构造函数
- Spring DI - 集合引用构造函数
- Spring DI - 地图构造器
- Spring DI - 地图引用构造函数
- 基于 Setter 的注入示例
- Spring DI - 基于 Setter
- Spring DI - 内部 Bean Setter
- Spring DI - 集合设置器
- Spring DI - 系列参考设置器
- Spring DI - 地图设置器
- Spring DI - 地图参考设置器
- 自动装配示例
- Spring DI - 自动装配
- Spring DI - 自动装配 ByName
- Spring DI - 按类型自动装配
- Spring DI - 自动装配构造函数
- 工厂方法
- Spring DI - 静态工厂
- Spring DI - 非静态工厂
- Spring DI 有用资源
- Spring DI - 快速指南
- Spring DI - 有用的资源
- Spring DI - 讨论
Spring DI - 集合设置器
您已经了解了如何使用Bean 配置文件中 <property> 标记的ref属性和值属性来配置原始数据类型和对象引用。这两种情况都涉及将奇异值传递给 bean。
现在,如果您想传递复数值,例如 Java 集合类型(例如 List、Set、Map 和 Properties),该怎么办?为了处理这种情况,Spring 提供了以下类型的集合配置元素,如下所示:
| 先生编号 | 元素和描述 |
|---|---|
| 1 |
<列表> 这有助于连接,即注入值列表,允许重复。 |
| 2 |
<设置> 这有助于连接一组值,但没有任何重复。 |
| 3 |
<道具> 这可用于注入名称-值对的集合,其中名称和值都是字符串。 |
您可以使用 <list> 或 <set> 来连接 java.util.Collection 或array的任何实现。
在此示例中,我们展示了传递集合元素的直接值。
例子
以下示例显示了一个JavaCollection类,该类使用集合作为使用 setter 注入的依赖项。
让我们更新一下Spring DI - 创建项目章节中创建的项目。我们正在添加以下文件 -
JavaCollection.java - 包含集合作为依赖项的类。
MainApp.java - 要运行和测试的主应用程序。
这是JavaCollection.java文件的内容-
package com.tutorialspoint;
import java.util.*;
public class JavaCollection {
List<String> addressList;
Set<String> addressSet;
Properties 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">
<!-- 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>JAPAN</value>
<value>USA</value>
<value>UK</value>
</list>
</property>
<!-- results in a setAddressSet(java.util.Set) call -->
<property name = "addressSet">
<set>
<value>INDIA</value>
<value>JAPAN</value>
<value>USA</value>
<value>UK</value>
</set>
</property>
<!-- results in a setAddressProp(java.util.Properties) call -->
<property name = "addressProp">
<props>
<prop key = "one">INDIA</prop>
<prop key = "two">JAPAN</prop>
<prop key = "three">USA</prop>
<prop key = "four">UK</prop>
</props>
</property>
</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}