在Spring中我们通过value属性来配置基本数据类型,通过标签的ref属性来配置对象的引用。这两种情况只能给bean传递一个值,那么如何传递多个值呢?Spring提供了四种Collection类型集合配置元素。

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

Spring.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="collection" class="com.test.spring.Collection">
<property name="list">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</list>
</property>
<property name="set">
<set>
<value>张三</value>
<value>李四</value>
<value>王五</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="张三"></entry>
<entry key="2" value="李四"></entry>
<entry key="3" value="王五"></entry>
</map>
</property>
<property name="proper">
<props>
<prop key="1">张三</prop>
<prop key="2">李四</prop>
<prop key="3">王五</prop>
</props>
</property>
</bean>
</beans>

Java代码:

package com.test.spring;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set; public class Collection {
private List list;
private Set set;
private Map map;
private Properties proper; public Properties getProper() {
return proper;
} public void setProper(Properties proper) {
this.proper = proper;
} public List getList() {
return list;
} public void setList(List list) {
this.list = list;
} public Set getSet() {
return set;
} public void setSet(Set set) {
this.set = set;
} public Map getMap() {
return map;
} public void setMap(Map map) {
this.map = map;
}
}

测试:

package com.test.spring;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class T {
AbstractApplicationContext applicationcontext=null;
@Before
public void before() {
System.out.println("》》》Spring ApplicationContext容器开始初始化了......");
applicationcontext= new ClassPathXmlApplicationContext(new String[]{"test1-service.xml"});
System.out.println("》》》Spring ApplicationContext容器初始化完毕了......");
}
@Test
public void test() {
collection=applicationcontext.getBean(Collection.class);
System.out.println(collection.getList());
System.out.println(collection.getSet());
System.out.println(collection.getMap());
System.out.println(collection.getProper());
}
}

测试结果:

》》》Spring ApplicationContext容器开始初始化了......
2017-03-19 15:49:39  INFO:ClassPathXmlApplicationContext-Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5facc36f: startup date [Sun Mar 19 15:49:39 CST 2017]; root of context hierarchy
2017-03-19 15:49:40  INFO:XmlBeanDefinitionReader-Loading XML bean definitions from class path resource [test1-service.xml]
》》》Spring ApplicationContext容器初始化完毕了......
[张三, 李四, 王五]
[张三, 李四, 王五]
{1=张三, 2=李四, 3=王五}
{3=王五, 2=李四, 1=张三}

Spring点滴八:Spring注入集合的更多相关文章

  1. Spring学习(八)-----Spring注入值到集合类型的例子

    下面例子向您展示Spring如何注入值到集合类型(List, Set, Map, and Properties). 支持4个主要的集合类型: List – <list/> Set – &l ...

  2. Spring中给Bean注入集合

    Spring中如果一个Bean里含有集合元素,需要给Bean里的集合元素注入元素时,可以采用如下方法,一个是构造器注入,一个是setter注入 JavaBean源代码: import java.uti ...

  3. Spring在Bean中注入集合

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/injecting-collection.html: 如果你想传递多个值,如Java Collect ...

  4. Spring的DI(Ioc) - 注入集合类型

    1: 首先给service添加集合类型的属性,并提供getter, setter package cn.gbx.serviceimpl; import java.util.ArrayList; imp ...

  5. spring IOC bean中注入集合

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

  6. 玩转spring MVC(八)----spring MVC整合json

    这篇文章在前边项目的基础上来整合json,最新项目资料见:http://download.csdn.net/detail/u012116457/8436187 首先需要的jar包:jackson-co ...

  7. spring入门(八) spring mvc设置默认首页

    1.web.xml配置如下 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  8. Spring Aop和Spring Ioc(二)

    Spring IOC: DI注入集合类型: 实体类: package cn.spring.entity; import java.util.*; public class Dientity { pri ...

  9. 【spring set注入 注入集合】 使用set注入的方式注入List集合和Map集合/将一个bean注入另一个Bean

    Dao层代码: package com.it.dao; public interface SayHell { public void sayHello(); } Dao的Impl实现层: packag ...

随机推荐

  1. C++箴言:理解typename的两个含义 nested dependent name(嵌套依赖名字)

    template<class T> class Widget; // uses "class"template<typename T> class Widg ...

  2. always on 集群

    准备工作 1.  四台已安装windows server 2008 r2 系统的虚拟机,配置如下: CPU : 1核 MEMORY : 2GB DISK : 40GB(未分区) NetAdapter ...

  3. storm报错:Exception in thread "main" java.lang.RuntimeException: InvalidTopologyException(msg:Component: [mybolt] subscribes from non-existent stream: [default] of component [kafka_spout])

    问题描述: storm版本:1.2.2,kafka版本:2.11.   在使用storm去消费kafka中的数据时,发生了如下错误. [root@node01 jars]# /opt/storm-1. ...

  4. 20155234 Exp2 后门原理与实践

    Windows获得Linux Shell 1.查看ip 2.监听端口 3.实验成功如下图 Linux获得Win Shell 1.查看虚拟机ip 2.监听端口 3.实验成功如下图 使用NC传输数据 1. ...

  5. Luogu P2055 [ZJOI2009]假期的宿舍

    一道网络有关的问题,还是一句话 网络流重在建模! 这里主要讲两种算法. 1.二分图匹配: 分析题意,我们可以知道题目要求是让所有留在学校的人都能有床睡 而 所有留在学校的人=本校不回家的人+外校的人: ...

  6. STM32---定时器的ETR功能

    定时器的ETR功能 在使用定时器的时候,在引脚复用功能中看到了TIM2_CH1_ETR,这个ETR是什么意思呢? 答:TIM2_CH1_ETR表示两个功能选一个,分别是TIM2_CH1和TIM2_ET ...

  7. flask前端与后端之间传递的两种数据格式:json与FormData

    json格式 双向! 前端 ==>后端:json格式 后端 ==>前端:json格式 html <!-- html部分 --> <form enctype='applic ...

  8. QTableWidget 详细使用

    QTableWidget 详细使用

  9. Kubernetes学习之路(二十三)之资源指标和集群监控

    目录 1.资源指标和资源监控 2.Weave Scope监控集群 (1)Weave Scope部署 (2)使用 Scope (3)拓扑结构 (4)实时资源监控 (5)在线操作 (6)强大的搜索功能 2 ...

  10. 【转】Spring Boot干货系列:(一)优雅的入门篇

    转自Spring Boot干货系列:(一)优雅的入门篇 前言 Spring一直是很火的一个开源框架,在过去的一段时间里,Spring Boot在社区中热度一直很高,所以决定花时间来了解和学习,为自己做 ...