以下内容引用自http://wiki.jikexueyuan.com/project/spring/injecting-collection.html

如果你想传递多个值,如Java Collection类型List、Set、Map和Properties,Spring 提供了四种类型的集合的配置元素,如下所示:

元素 描述
<list> 它有助于连线,如注入一列值,允许重复。
<set> 它有助于连线一组值,但不能重复。
<map> 它可以用来注入键值对的集合,其中键和值可以是任何类型。
<props> 它可以用来注入键值对的集合,其中键和值都是字符串类型。

您可以使用<list>或<set>来连接java.util.Collection的任何实现或数组。您将遇到两种情况(a)传递集合的直接值,(b)将bean的引用作为集合元素之一传递。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId>
<artifactId>testinjectioncollection</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>testinjectioncollection</name>
<url>http://maven.apache.org</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring Core -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.4.RELEASE</version>
</dependency> <!-- Spring Context -->
<!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>

JavaCollection.java:

package com.jsoft.testspring.testinjectioncollection;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class JavaCollection {
private List addressList;
private Set addressSet;
private Map addressMap;
private Properties addressProp; public void setAddressList(List addressList) {
this.addressList = addressList;
} public List getAddressList() {
System.out.println("List :" + addressList);
return addressList;
} public void setAddressSet(Set addressSet) {
this.addressSet = addressSet;
} public Set getAddressSet() {
System.out.println("Set :" + addressSet);
return addressSet;
} public void setAddressMap(Map addressMap) {
this.addressMap = addressMap;
} public Map getAddressMap() {
System.out.println("Map :" + addressMap);
return addressMap;
} public void setAddressProp(Properties addressProp) {
this.addressProp = addressProp;
} public Properties getAddressProp() {
System.out.println("Property :" + addressProp);
return addressProp;
}
}

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.jsoft.testspring.testinjectioncollection.JavaCollection"> <!-- list -->
<property name="addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</list>
</property> <!-- set -->
<property name="addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
</set>
</property> <!-- 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> <!-- 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>

App.java:

package com.jsoft.testspring.testinjectioncollection;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* Hello world!
*
*/
public class App
{
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();
}
}

运行结果:

注入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.xsd"> <bean id="address1" class="com.jsoft.testspring.testinjectioncollection.Address"></bean>
<bean id="address2" class="com.jsoft.testspring.testinjectioncollection.Address"></bean>

<bean id="javaCollection" class="com.jsoft.testspring.testinjectioncollection.JavaCollection"> <!-- list -->
<property name="addressList">
<list>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
<ref bean="address1"/>
<ref bean="address2"/>

</list>
</property> <!-- set -->
<property name="addressSet">
<set>
<value>INDIA</value>
<value>Pakistan</value>
<value>USA</value>
<value>USA</value>
<ref bean="address1"/>
<ref bean="address2"/>

</set>
</property> <!-- 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" />
<entry key="5" value-ref="address1" />
<entry key="6" value-ref="address2" />

</map>
</property> <!-- 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>

可以看出,除了property类型外,全部都可以注入bean。因为property类型键值都只能是字符串类型,所以没办法使用对象。

注入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)。

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test10/testinjectioncollection

Spring在Bean中注入集合的更多相关文章

  1. spring IOC bean中注入集合

    建立一个实体 package com.java.test4; import java.util.*; /** * @author nidegui * @create 2019-06-22 14:45 ...

  2. 在Spring的bean中注入HttpServletRequest解密

    我们可以在Spring的bean中轻松的注入HttpServletRequest,使用@Autowired HttpServletRequest request;就可以了. 但是,为什么我们可以直接这 ...

  3. (转载)在spring的bean中注入内部类

    原文链接:http://outofmemory.cn/java/spring/spring-DI-inner-class 在spring中注入内部类,有可能会遇到如下异常信息: 2014-5-14 2 ...

  4. spring IOC bean中注入bean

    俩个实体 package com.java.test4; /** * @author nidegui * @create 2019-06-22 14:45 */ public class People ...

  5. Spring IOC容器中注入bean

    一.基于schema格式的注入 1.基本的注入方式 (属性注入方式) 根据setXxx()方法进行依赖注入,Spring只会检查是否有setter方法,是否有对应的属性不做要求 <bean id ...

  6. 【转】spring 装配Bean中构造参数的注入

    转载自:http://www.bianceng.cn/Programming/Java/201307/37027.htm spring 装配Bean中构造参数的注入 spring装配bean中还有一种 ...

  7. Spring在Thread中注入Bean无效的解决方式

    在Spring项目中,有时需要新开线程完成一些复杂任务,而线程中可能需要注入一些服务.而通过Spring注入来管理和使用服务是较为合理的方式.但是若直接在Thread子类中通过注解方式注入Bean是无 ...

  8. spring中如何向一个单例bean中注入非单例bean

    看到这个题目相信很多小伙伴都是懵懵的,平时我们的做法大都是下面的操作 @Component public class People{ @Autowired private Man man; } 这里如 ...

  9. spring给容器中注入组件的几种方式

    目录 环境搭建 spring给容器中注入组件 1.包扫描+组件标注注解(@Controller/@Service/@Repository/@Component)适用于把自己写的类加入组件(默认ID类名 ...

随机推荐

  1. VS搭建一个WEB的简历第二天,,,最终目标写个好看的简历,再搭建一个自己脑海的网页

    VS做简历的第二天 第二天吸取了第一天的教训写的代码 第一天写的代码https://www.cnblogs.com/pythonywy/p/10816215.html,写了一堆错误T T 非常感谢Li ...

  2. cdev_add

    初始化 cdev 后,需要把它添加到系统中去.为此可以调用 cdev_add()函数.传入cdev 结构的指针,起始设备编号,以及设备编号范围. 函数首先将分配的设备号与设备数目保存进cdev结构体中 ...

  3. C/C++编程之内存管理

    内存分配方式 内存分配方式一共有三种: (1)从静态存储区域分配: 内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在,例如,全局变量,静态变量. (2)在栈上创建: 在执行函数时, ...

  4. C盘清理小技巧

    步骤/方法 1 1  关闭休眠功能,在开始菜单的运行里输入powercfg -h off 指令,关闭休眠,此文件实际大小和物理内存是一样的,大约可以为C盘释放1-3G的空间. 2 2  设置虚拟内存: ...

  5. xshell连接linux

    一些命令和快捷键: Ctrl + Alt 切换linux和windows的鼠标 Ctrl + c 或 Ctrl + d退出>状态 在xshell终端输入exit,退出与linux服务器的连接 登 ...

  6. volatile随笔见解

    1.volatile可以保证可见性,不能保证一致性,但是与cas操作结合在实现并发上性能很不错,java并发包下不少类都有这种实现方式. 2.相比synchronized执行成本更低,因为它不会引起线 ...

  7. luogu2526 [SHOI2001]小狗散步

    注意一个景点只能去一次. #include <iostream> #include <cstring> #include <cstdio> #include < ...

  8. 【LeetCode】Roman to Integer(罗马数字转整数)

    这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...

  9. HDU-5317 RGCDQ ,暴力打表!

    RGCDQ 暴力水题,很可惜比赛时没有做出来,理清思路是很简单的. 题意:定义f(i)表示i的素因子个数,给你一段区间[l,r],求max_gcd(f(i),f(j)).具体细节参考题目. 思路:数据 ...

  10. 九度oj 题目1345:XXX定律之画X

    题目描述: 给你一个n,然后让你输出F(n)规则是这样的,F(n)的输出结果是:F(n-1)     F(n-1)       F(n-1) F(n-1)      F(n-1) F(1)的输出结果是 ...