在Spring中可以装配4种集合类型属性:List、set、Map和Properties。与这四种集合对应的标签是、、、。CollectionBean是一个包含上述4种集合类型的JavaBean,代码如下:

package chapter22;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class CollectionBean {private List myList;private String myArray[];private Set mySet;private Map myMap;private Properties myProperties;public List getMyList() {return myList;}public void setMyList(List myList) {System.out.println("set List类型:"+myList.getClass().getName());this.myList = myList;}public String[] getMyArray() {return myArray;}public void setMyArray(String[] myArray) {this.myArray = myArray;}public Set getMySet() {return mySet;}public void setMySet(Set mySet) {System.out.println("set Set类型:"+mySet.getClass().getName());this.mySet = mySet;}public Map getMyMap() {return myMap;}public void setMyMap(Map myMap) {System.out.println("set Map类型:"+myMap.getClass().getName());this.myMap = myMap;}public Properties getMyProperties() {return myProperties;}public void setMyProperties(Properties myProperties) {System.out.println("set Properties类型:"+myProperties.getClass().getName());this.myProperties = myProperties;}
}
下面是装配上述4中集合类型属性的配置代码
xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean id="helloservice" class="chapter22.HelloServiceImpl"><property name="greeting" value="greeting Xu Wei">property>bean><bean id="myBean" class="chapter22.MyBean"><property name="hello"><ref bean="helloservice" />property><property name="strName" value="Xu Wei">property>bean> <bean id="CollectionBean" class="chapter22.CollectionBean"> <property name="myList"> <list><value>abcdvalue> <idref bean="myBean">idref> list> property><property name="myMap"> <map> <entry> <key> <value>hellovalue>key> <value>1234value>entry> <entry key="abcd"> <ref bean="helloservice" />entry> <entry> <key> <ref bean="helloservice" >ref>key> <ref bean="myBean" >ref>entry>map>property> <property name="mySet"> <set> <value>testvalue> <ref bean="myBean">ref>set>property> <property name="myProperties"> <props><prop key="abcd">value1prop> <prop key="prop">myPropprop>props>property> <property name="myArray"> <list> <value>myArrayvalue> <idref bean="helloservice" >idref>list>property>bean> beans>

下面的TestCollectionContext.java代码使用ApplicationContext接口的getBean方法创建了CollectionBean对象,并访问了相关属性。

package chapter22;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;public class TestCollectionBean {public static void main(String args[]){ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml");
/*在创建ctx对象实例以后,applicationContext.xml配置中涉及到的Bean的set方法都被调用了。*单单执行ApplicationContext ctx=new FileSystemXmlApplicationContext("src//applicationContext.xml");*的输出结果是:*log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).*log4j:WARN Please initialize the log4j system properly.*setGreeting()方法被调用*setHello()方法被调用*setStrName()方法被调用*set List类型:java.util.ArrayList*set Map类型:java.util.LinkedHashMap*set Set类型:java.util.LinkedHashSet*set Properties类型:java.util.Properties*/    //创建CollectionBean对象cb。CollectionBean cb=(CollectionBean)ctx.getBean("CollectionBean");System.out.println("------测试List------");for(String s:cb.getMyList())System.out.println(s);System.out.println("------测试Array------");for(String s:cb.getMyArray())System.out.println(s);System.out.println("------测试Set------");for(Object obj:cb.getMySet())System.out.println(obj.getClass().getName());System.out.println("------测试Map------");//Map其实就是key-value的键值对。cb.getMyMap().get(key)是要找到key所对应的value。for(Object key:cb.getMyMap().keySet())System.out.println(key+"="+cb.getMyMap().get(key));System.out.println("------测试Properties------");for(Object key:cb.getMyProperties().keySet())System.out.println(key+"="+cb.getMyProperties().get(key));}
}

输出结果:

log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
setGreeting()方法被调用
setHello()方法被调用
setStrName()方法被调用
set List类型:java.util.ArrayList
set Map类型:java.util.LinkedHashMap
set Set类型:java.util.LinkedHashSet
set Properties类型:java.util.Properties
------测试List------
abcd
myBean
------测试Array------
myArray
helloservice
------测试Set------
java.lang.String
chapter22.MyBean
------测试Map------
hello=1234
abcd=chapter22.HelloServiceImpl@e70e30
chapter22.HelloServiceImpl@e70e30=chapter22.MyBean@154864a
------测试Properties------
abcd=value1
prop=myProp

转载于:https://www.cnblogs.com/xwdreamer/archive/2010/09/12/2297093.html

Spring装配集合属性相关推荐

  1. Spring装配Bean---使用xml配置

    声明Bean Spring配置文件的根元素是<beans>. 在<beans>元素内,你可以放所有的Spring配置信息,包括<bean>元素的声明. 除了Bean ...

  2. spring项目属性注入和bean管理xml 注入一般属性和集合属性

    IOC 介绍: 在Spring的应用中,Spring IoC容器可以创建.装配和配置应用组件对象,这里的组件对象称为Bean. Bean的实例化 在面向对象编程中,想使用某个对象时,需要事先实例化该对 ...

  3. java map 数组_java技术Spring集合属性

    集合属性 在Spring中可以通过一组内置的XML标签来配置集合属性,例如:<list>,<set>或<map>. 1 数组和List 配置java.util.Li ...

  4. Spring框架中集合属性为对象的注入方法

    Spring框架中集合属性为对象的注入方法 前言 创建基础类 创建`Course`类 编写XML配置文件 创建测试类 执行结果 前言 在集合的属性注入中,如果注入属性为普通类型(String.int) ...

  5. 【Spring】 IOC应用 【4】注入集合属性

    注入集合属性 环境: 定义一个Mul类 数组 配置文件中,关于数组的注入如下: <bean id="Mul" class="Day7.Bean.Mul"& ...

  6. [Spring] 注入Bean属性

    通常,JavaBean的属性是私有的,同时拥有一组存取器方法,setXXX()和getXXX()形式存在.Spring可以借助属性的set方法来配置属性的值,以实现setter方式的注入. Kenny ...

  7. Spring - 装配bean

    创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入(DI)的本质 一.自动化装配bean(推荐方式) Spring从两个角度来实现自动化装配: 组件扫描:Spring会自动发现 ...

  8. 这篇文章,我们来谈一谈Spring中的属性注入

    本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring中的对象跟Bean,你知道Spring怎么创 ...

  9. spring装配Bean过程

    主要流程: 1.读取配置文件 2.实例化bean和填充bean属性 这个粗略的流程感觉更像是一个需求,有了这个需求,那么spring内部是怎么处理的呢? 我们知道spring的两个核心接口BeanFa ...

  10. JAVA_OA管理系统(四)番外篇:使用Spring注解注入属性

    本文介绍了使用Spring注解注入属性的方法.使用注解以前,注入属性通过类以及配置文件来实现.现在,注入属性可以通过引入@Autowired注解,或者@Resource,@Qualifier,@Pos ...

最新文章

  1. linux mysql授权外部访问权限,Linux中安装Mysql授权远程访问
  2. arm linux ping 通百度,linux ---之与ARM开发板相互ping 通
  3. 论文总结:Fast and Light Bandwidth Testing for Internet Users(21‘ NSDI)
  4. caffe学习笔记--跑个SampleCode
  5. 哈夫曼字符串编码c语言实现,基于哈夫曼(haffuman)算法的文件压缩的实现(C语言)(原创)...
  6. Gym-100935I Farm 计算几何 圆和矩形面积交
  7. HTTP中CORS跨域请求的实现(C++|Qt框架实现)
  8. 网传梅姨照片竟然是电脑合成的!仅需 100 行代码,你也能做到!
  9. 谷歌提出深度CNN模型NIMA:帮你挑选清晰且有美感的图片
  10. 编程语言python怎么读-Python和Go都很火,我要怎么选?
  11. 【语言处理与Python】1.3计算语言:简单的统计
  12. Win10系统重装教程(纯净版)
  13. 不积跬步无以至千里(C语言笔记)
  14. bitcode 是什么_secured是什么意思 Secured borrowings是什么意思
  15. B360主板i5 8400装Win7记录
  16. JS鼠标放上移开 显示隐藏图标 的代码思路
  17. okcc呼叫中心系统防封号系统的工作原理
  18. 如何启动Intel VT-x
  19. java从入门到出轨
  20. 换地方上网后Kali Linux 网络设置

热门文章

  1. 难道现在是保险业的高速发展期?
  2. 使用Redis存取数据+数据库存取(spring+java)
  3. UVA 1637 Double Patience
  4. Java NIO之缓冲区Buffer
  5. 布同:后台开发入职四年的经历和体会
  6. 如何用C#代码判断一个类的类型
  7. ESX的VSWITCH坏了,如何转移到新建的虚拟交换机上?
  8. VS2005 中网站转为Web应用程序的方法
  9. 075 json和pickle模块
  10. 小明买了一箱鸡蛋,假设有n个,可以一天吃1个,也可以一天吃2个,请问有多 少种方法可以吃完?...