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

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

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

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

  1. public interface PersonService {
  2. Set<String> getSets();
  3. List<String> getLists();
  4. Properties getProperties();
  5. Map<String, String> getMaps();
  6. void save();
  7. }

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

  1. public class PersonServiceBean implements PersonService {
  2. private Set<String> sets = new HashSet<String>();
  3. private List<String> lists = new ArrayList<String>();
  4. private Properties properties = new Properties();
  5. private Map<String, String> maps = new HashMap<String, String>();
  6. public Map<String, String> getMaps() {
  7. return maps;
  8. }
  9. public void setMaps(Map<String, String> maps) {
  10. this.maps = maps;
  11. }
  12. public Properties getProperties() {
  13. return properties;
  14. }
  15. public void setProperties(Properties properties) {
  16. this.properties = properties;
  17. }
  18. public List<String> getLists() {
  19. return lists;
  20. }
  21. public void setLists(List<String> lists) {
  22. this.lists = lists;
  23. }
  24. public Set<String> getSets() {
  25. return sets;
  26. }
  27. public void setSets(Set<String> sets) {
  28. this.sets = sets;
  29. }
  30. @Override
  31. public void save() {
  32. }
  33. }
  • 1

然后将Spring的配置文件——beans.xml的内容置为:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
  7. <property name="sets">
  8. <set>
  9. <value>第一个</value>
  10. <value>第二个</value>
  11. <value>第三个</value>
  12. </set>
  13. </property>
  14. <property name="lists">
  15. <list>
  16. <value>第一个list元素</value>
  17. <value>第二个list元素</value>
  18. <value>第三个list元素</value>
  19. </list>
  20. </property>
  21. <property name="properties">
  22. <props>
  23. <prop key="key1">value1</prop>
  24. <prop key="key2">value2</prop>
  25. <prop key="key3">value3</prop>
  26. </props>
  27. </property>
  28. <property name="maps">
  29. <map>
  30. <entry key="key-1" value="value-1"></entry>
  31. <entry key="key-2" value="value-2"></entry>
  32. <entry key="key-3" value="value-3"></entry>
  33. </map>
  34. </property>
  35. </bean>
  36. </beans>
  • 1

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

  1. public class SpringTest {
  2. @Test
  3. public void instanceSpring() {
  4. AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  5. PersonService personService = (PersonService) ctx.getBean("personService");
  6. System.out.println("===================set===================");
  7. for (String value : personService.getSets()) {
  8. System.out.println(value);
  9. }
  10. System.out.println("===================list===================");
  11. for (String value : personService.getLists()) {
  12. System.out.println(value);
  13. }
  14. System.out.println("===================properties===================");
  15. for (Object key : personService.getProperties().keySet()) {
  16. System.out.println(key + "=" + personService.getProperties().getProperty((String) key));
  17. }
  18. System.out.println("===================maps===================");
  19. for (Object key : personService.getMaps().keySet()) {
  20. System.out.println(key + "=" + personService.getMaps().get(key));
  21. }
  22. ctx.close();
  23. }
  24. }
  • 1

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

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

使用构造器装配属性

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

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

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

  1. public interface PersonService {
  2. void save();
  3. }

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

  1. public class PersonServiceBean implements PersonService {
  2. private PersonDao personDao;
  3. private String name;
  4. public PersonServiceBean(PersonDao personDao, String name) {
  5. this.personDao = personDao;
  6. this.name = name;
  7. }
  8. @Override
  9. public void save() {
  10. System.out.println(name);
  11. personDao.add();
  12. }
  13. }
  • 1

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <bean id="personDao" class="cn.itcast.dao.impl.PersonDaoBean" />
  7. <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
  8. <constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao" />
  9. <constructor-arg index="1" value="李阿昀" />
  10. <!--
  11. <property name="sets">
  12. <set>
  13. <value>第一个</value>
  14. <value>第二个</value>
  15. <value>第三个</value>
  16. </set>
  17. </property>
  18. <property name="lists">
  19. <list>
  20. <value>第一个list元素</value>
  21. <value>第二个list元素</value>
  22. <value>第三个list元素</value>
  23. </list>
  24. </property>
  25. <property name="properties">
  26. <props>
  27. <prop key="key1">value1</prop>
  28. <prop key="key2">value2</prop>
  29. <prop key="key3">value3</prop>
  30. </props>
  31. </property>
  32. <property name="maps">
  33. <map>
  34. <entry key="key-1" value="value-1"></entry>
  35. <entry key="key-2" value="value-2"></entry>
  36. <entry key="key-3" value="value-3"></entry>
  37. </map>
  38. </property>
  39. -->
  40. </bean>
  41. </beans>
  • 1

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

  1. public class SpringTest {
  2. @Test
  3. public void instanceSpring() {
  4. AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
  5. PersonService personService = (PersonService) ctx.getBean("personService");
  6. personService.save();
  7. ctx.close();
  8. }
  9. }

测试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. Bootstrap-CSS:排版

    ylbtech-Bootstrap-CSS:排版 1.返回顶部 1. Bootstrap 排版 Bootstrap 使用 Helvetica Neue. Helvetica. Arial 和 sans ...

  2. css3 实现png图片改变背景颜色

    实际上是用的是就是css的filter的drop-shadow属性 drop-shadow: 1 不支持内阴影 2 不支持多阴影 3 兼容性 ie13+  谷歌 火狐   android4.4+  i ...

  3. 最优灌溉_最小生成树Kruskal

    问题描述 雷雷承包了很多片麦田,为了灌溉这些麦田,雷雷在第一个麦田挖了一口很深的水井,所有的麦田都从这口井来引水灌溉. 为了灌溉,雷雷需要建立一些水渠,以连接水井和麦田,雷雷也可以利用部分麦田作为“中 ...

  4. 3、HTML的body内标签1

    一.特殊符号的表示   #代指空格 < #代指,< > #代指,> ...... #这玩意有很多,记也记不完,用的时候查一下即可: 二.p和br标签 <p>< ...

  5. 任务48:Identity MVC:Model后端验证

    任务48:Identity MVC:Model后端验证 RegisterViewModel using System; using System.Collections.Generic; using ...

  6. Codeforces510B【dfs】

    判断一个图里是否有一个自环: 50*50 标记起点,然后暴搜? #include <bits/stdc++.h> #include<algorithm> using names ...

  7. lightoj 1031【区间DP,未完待续】

    题意: 给你一个n,再给你n个数,每个数<1e4; 有两个player交替取数字,每个人每一次能拿一个或多个,交替在两边拿. 游戏终止在所有的数字被取完. 两个人的分数就是所取得的数字大小总和. ...

  8. poj 3415 Common Substrings【SA+单调栈】

    把两个串中间加一个未出现字符接起来,然后求SA 然后把贡献统计分为两部分,在排序后的后缀里,属于串2的后缀和排在他前面属于串1的后缀的贡献和属于串1的后缀和排在他前面属于串2的后缀的贡献 两部分分别作 ...

  9. Luogu P2114[NOI2014]起床困难综合症 【贪心/位运算】By cellur925

    题目传送门 所以NOI的题现在简单惹? 30分做法:枚举开始的权值,n²过掉. 100分做法:竟然是贪心qwq.因为我们的计算背景是二进制下,所以我们贪心地想让每一位都是1.我们现在需要解决的问题,就 ...

  10. oauth2(spring security)报错method_not_allowed(Request method 'GET' not supported)解决方法

    报错信息 <MethodNotAllowed> <error>method_not_allowed</error> <error_description> ...