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 {@Testpublic 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 {@Testpublic void instanceSpring() { AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService = (PersonService) ctx.getBean("personService"); personService.save(); ctx.close(); } }

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

转载于:https://www.cnblogs.com/telwanggs/p/6913172.html

(转)Spring如何装配各种集合类型的属性相关推荐

  1. JSON字符串转换object错误:MorphDynaBean cannot be cast to com.softright.bean.TestBean,类中有集合类型的属性...

    今天遇到个错误 因为JSONObject.toBean()把JSON字符串转换为一个自己定义的类,当其中属性有类似List , Map ,ArrayList的时候,麻烦就来了 错误:MorphDyna ...

  2. Spring.NET学习笔记8——集合类型的注入(基础篇) Level 200

    Spring.NET还支持集合类型的注入.而且使用起来也比较方便. 一.ILIst类型 使用<list>元素作为ILIst的标签,value为集合中元素的值.也可以注入对象,甚至关联其它对 ...

  3. 一步一步手绘Spring DI运行时序图(Spring 自动装配之依赖注入)

    相关内容: 架构师系列内容:架构师学习笔记(持续更新) 一步一步手绘Spring IOC运行时序图一(Spring 核心容器 IOC初始化过程) 一步一步手绘Spring IOC运行时序图二(基于XM ...

  4. Spring-注入参数详解-[通过util命名空间简化集合类型的配置]

    概述 步骤 声明命名空间和schema 配置Bean 配置一个Map 配置一个Set 配置一个List 配置一个Properties MapSetListProperties实例汇总 概述 如果希望配 ...

  5. Spring 自动装配 ‘byType’

    转载自   Spring 自动装配 'byType' Spring 自动装配 'byType' 这种模式由属性类型指定自动装配.Spring 容器看作 beans,在 XML 配置文件中 beans ...

  6. spring自动装配、注解

    spring自动装配 Spring 自动装配 byName 这种模式由属性名称指定自动装配.Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 ...

  7. Spring 自动装配 ‘byName’

    转载自  Spring 自动装配 'byName' Spring 自动装配 'byName' 这种模式由属性名称指定自动装配.Spring 容器看作 beans,在 XML 配置文件中 beans 的 ...

  8. Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器

    1. 概要 struts2:web hibernate:持久化 spring:业务层.管理bean的,容器.List Map Set.体验spring:1.创建java项目.2.引入spring的类库 ...

  9. spring中的依赖注入——构造函数注入、set方法注入( 更常用的方式)、复杂类型的注入/集合类型的注入

    spring中的依赖注入 依赖注入: Dependency Injection IOC的作用:降低程序间的耦合(依赖关系) 依赖关系的管理:以后都交给spring来维护.在当前类需要用到其他类的对象, ...

最新文章

  1. nginx 访问日志分析工具 goacess
  2. GNU/Linux的历史
  3. Factom(公证通)--基于区块链的存证系统
  4. 大顶堆删除最大值_C++|使用STL算法创建、调整、输出最大堆、最小堆
  5. cs1.5 linux服务端,CS1.5在linux上的配置
  6. nginx 修改 max open files limits
  7. css中的背景、边框、补丁相关属性
  8. 关于HTML条件注释你可能不知道的一些事儿
  9. 关于分行数字化转型工作的几点思考
  10. YOLOv2论文理解
  11. numpy数组随机抽取
  12. matlab 元胞数组
  13. 在线多功能工具箱php源码
  14. 计算机电源德国产,德国原装崇拜者来一发?BeQuiet! Straight Power 11全模电源
  15. 请各位大虾帮忙!小女子谢过了!:)
  16. 模拟电路 二极管门电路(二)
  17. 03-SQLPlus的常用命令和使用
  18. Jasper导出excel
  19. 黑盒测试用例设计--题目3
  20. Java实现远程主机唤醒 (WOL)

热门文章

  1. HDFS Federation机制
  2. (189)FPGA变量初始化方法reg
  3. (43)生成时钟Generated Clock简介
  4. (22)System Verilog按时间顺序的通知需求(事件驱动)
  5. System verilog随机系统函数$random使用方法
  6. java redis rpush_Redis Rpush命令
  7. 3.FreeRTOS学习笔记-任务
  8. 移植RTT使用cubeMx配置后出现 cannot open source input file stm32f1xx_hal_exti.h: No such file or directory
  9. 7004.vue脚手架快速生成项目
  10. QGIS2.18二次开发环境搭建--番外篇