http://blog.csdn.net/yerenyuan_pku/article/details/52858499

在前面我们已经会注入基本类型对象和其他bean,现在我们就来学习如何注入各种集合类型。

Spring如何装配各种集合类型的属性

首先新建一个普通的Java Project,名称为spring_collection,并迅速搭建好Spring的开发环境。 
接着在src目录下新建一个cn.itcast.service包,并在该包下创建PersonService接口,其代码为:

public interface PersonService {
Set<String> getSets(); List<String> getLists(); Properties getProperties(); Map<String, String> getMaps(); void save(); }

再接下来仍在src目录下新建一个cn.itcast.service.impl包,并在该包下创建PersonService接口的实现类——PersonServiceBean.java,其代码为:

public class PersonServiceBean implements PersonService {
private Set<String> sets = new HashSet<String>();
private List<String> lists = new ArrayList<String>();
private Properties properties = new Properties();
private Map<String, String> maps = new HashMap<String, String>(); public Map<String, String> getMaps() {
return maps;
} public void setMaps(Map<String, String> maps) {
this.maps = maps;
} public Properties getProperties() {
return properties;
} public void setProperties(Properties properties) {
this.properties = properties;
} public List<String> getLists() {
return lists;
} public void setLists(List<String> lists) {
this.lists = lists;
} public Set<String> getSets() {
return sets;
} public void setSets(Set<String> sets) {
this.sets = sets;
} @Override
public void save() { }
}
  • 1

然后将Spring的配置文件——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="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一个list元素</value>
<value>第二个list元素</value>
<value>第三个list元素</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key-1" value="value-1"></entry>
<entry key="key-2" value="value-2"></entry>
<entry key="key-3" value="value-3"></entry>
</map>
</property>
</bean> </beans>
  • 1

最后,在src目录下新建一个junit.test包,并在该包下新建一个单元测试类——SpringTest.java,其代码为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
System.out.println("===================set===================");
for (String value : personService.getSets()) {
System.out.println(value);
}
System.out.println("===================list===================");
for (String value : personService.getLists()) {
System.out.println(value);
}
System.out.println("===================properties===================");
for (Object key : personService.getProperties().keySet()) {
System.out.println(key + "=" + personService.getProperties().getProperty((String) key));
}
System.out.println("===================maps===================");
for (Object key : personService.getMaps().keySet()) {
System.out.println(key + "=" + personService.getMaps().get(key));
}
ctx.close();
} }
  • 1

测试instanceSpring()方法,会发现Eclipse的控制台打印: 

如要查看源码,可点击Spring如何装配各种集合类型的属性进行下载。

使用构造器装配属性

前面我们就已讲过spring的依赖注入有两种方式:

  1. 使用构造器注入。
  2. 使用属性setter方法注入。

我们已经详解过使用属性setter方法注入这种方式,接下来自然就到了使用构造器注入属性了。 
首先将PersonService接口的代码改为:

public interface PersonService {

    void save();

}

接着将PersonServiceBean实现类的代码修改为:

public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name; public PersonServiceBean(PersonDao personDao, String name) {
this.personDao = personDao;
this.name = name;
} @Override
public void save() {
System.out.println(name);
personDao.add();
}
}
  • 1

然后将Spring的配置文件修改为:

<?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="personDao" class="cn.itcast.dao.impl.PersonDaoBean" />
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao" />
<constructor-arg index="1" value="李阿昀" />
<!--
<property name="sets">
<set>
<value>第一个</value>
<value>第二个</value>
<value>第三个</value>
</set>
</property>
<property name="lists">
<list>
<value>第一个list元素</value>
<value>第二个list元素</value>
<value>第三个list元素</value>
</list>
</property>
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
<prop key="key3">value3</prop>
</props>
</property>
<property name="maps">
<map>
<entry key="key-1" value="value-1"></entry>
<entry key="key-2" value="value-2"></entry>
<entry key="key-3" value="value-3"></entry>
</map>
</property>
-->
</bean> </beans>
  • 1

最后,将单元测试类——SpringTest.java的代码改为:

public class SpringTest {

    @Test
public void instanceSpring() {
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService) ctx.getBean("personService");
personService.save();
ctx.close();
} }

测试instanceSpring()方法,可看到Eclipse控制台打印: 

 
 

(转)Spring如何装配各种集合类型的属性的更多相关文章

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

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

  2. Spring框架学习04——复杂类型的属性注入

    代码示例如下: 创建BeanClass实体类 public class BeanClass { private String[] arrs;//数组类型 private List<String& ...

  3. MyBatis(十一) 嵌套结果集的方式,使用collection标签定义关联的集合类型的属性封装规则

    (1)接口中编写方法 public Dept getDeptPlusById(Integer id); (2)Mapper文件 <resultMap type="com.eu.bean ...

  4. Spring中集合类型属性注入

    我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看. 这里将介绍如何给Map list set Array Properties 这些属性注入值. ...

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

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

  6. Spring自动装配之依赖注入(DI)

    依赖注入发生的时间 当Spring IOC 容器完成了Bean 定义资源的定位.载入和解析注册以后,IOC 容器中已经管理类Bean定义的相关数据,但是此时IOC 容器还没有对所管理的Bean 进行依 ...

  7. Ioc和Aop扩展--多种方式实现依赖注入(构造注入,p命名空间注入,集合类型注入,注入null和注入空值)

    构造注入 语法: <constructor-arg>    <ref bean="bean的id"/> </constructor-arg> 1 ...

  8. Spring、基本类型属性和集合类型属性的注入

    Spring 还可以对基本属性和集合类型属性进行注入: public interface PersonIService { public String getBaseProperty(); publi ...

  9. [Spring学习笔记 2 ]装配各种类型的属性 map,list,array,null,properties

    一.spring Ioc容器补充(1) Spring Ioc容器 DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入field(注解) ...

随机推荐

  1. How to study Watir?

    我在群空间,总是看到很多Watir新手,反复的问:我对元素的定位怎么又出错?我该从哪里着手啊?我给一个我认为最简单方便的学习方法.1. Ruby语法基础要入门,网上有一个不到2M的帮助文档,从头到尾仔 ...

  2. 使用strtok_s函数从一个字符串中分离出单词

    下面的代码从含有多个结束符的字符串中分离出单词来,需要对strtok_s有清楚的认识.这段代码是我在写一个处理文件中单词个数时用来分离读取到的字符串中的单词时写的,亲测可用~ 1 2 3 4 5 6 ...

  3. 【转】implements百科

    implements是一个类,实现一个接口用的关键字,它是用来实现接口中定义的抽象方法.实现一个接口,必须实现接口中的所有方法.   中文名 实现 外文名 implements 意    思 类实现一 ...

  4. Centos添加jdk环境变量

    假设将jdk解压到/opt/jdk1.8.0_131. echo "export JAVA_HOME=/opt/jdk1.8.0_131" >> /etc/profil ...

  5. bzoj 4503: 两个串【脑洞+FFT】

    真实脑洞题 因为通配符所以导致t串实际有指数级别个,任何字符串相关算法都没有用 考虑一个新的匹配方法:设a串(模板串)长为n,从m串的i位置开始匹配:\( \sum_{i=0}^{n-1}(a[j]- ...

  6. bzoj 3613: [Heoi2014]南园满地堆轻絮【二分+贪心】

    二分答案w,然后判断的时候维护一个mx,扫描序列,先更新mx=max(mx,a[i]-w),然后如果a[i]+w<mx的话就是说这个位置即使升到极限并且前面降到极限也不能符合条件了 #inclu ...

  7. Django models数据库配置以及多数据库调用设置

    今天来说说web框架Django怎么配置使用数据库,也就是传说中MVC(Model View Controller)中的M,Model(模型). 简单介绍一下Django中的MVC: 模型(model ...

  8. Jmeter之聚合报告

    1.添加线程组,添加请求接口 2.设置线程组 3.线程组右击添加—>监听器—>聚合报告

  9. python实现希尔排序

    与插入排序的思想一致,插入排序是一个,希尔排序是多个插入排序! # @File: shell_sort import random def insert_sort_gap(li, d): for i ...

  10. 线段树(单点更新) POJ 2828 Buy tickets

    题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...