Spring中集合定义

简介:

习惯了把集合定义在一个类的字段中,今天遇到想重用集合的定义,竟然不知道单独的集合Bean应该怎么定义了,记之,以备后用。

对Map来说,有一种比较搓的方法,就是直接用Map的构造函数:

 1 < bean id = " SymbolMap "   class = " java.util.HashMap " >   
 2      < constructor - arg >   
 3         < map >   
 4           < entry >   
 5              < key >< value ><! [CDATA[us;djia]] ></ value ></ key >   
 6              < value ><! [CDATA[us & dji]] ></ value >   
 7           </ entry >   
 8         </ map >   
 9      </ constructor - arg >   
10 </ bean >   
11

另一种稍微简单的方法:

 1 < bean id = " emails "   class = " org.springframework.beans.factory.config.MapFactoryBean " >   
 2    < property name = " sourceMap " >   
 3        < map >   
 4          < entry key = " pechorin "  value = " pechorin@hero.org " />   
 5          < entry key = " raskolnikov "  value = " raskolnikov@slums.org " />   
 6          < entry key = " stavrogin "  value = " stavrogin@gov.org " />   
 7          < entry key = " porfiry "  value = " porfiry@gov.org " />   
 8        </ map >   
 9    </ property >   
10 </ bean >   
11

对这种方法,Sping还提供了ListFactoryBean, SetFactoryBean等类,这貌似是Spring的一个可扩展框架,可以待以后进一步研究这个框架的实现方式。
最简单的一种方式就是直接用spring中提供的util包:

1 < util:map id = " emails " >   
2      < entry key = " pechorin "  value = " pechorin@hero.org " />   
3      < entry key = " raskolnikov "  value = " raskolnikov@slums.org " />   
4      < entry key = " stavrogin "  value = " stavrogin@gov.org " />   
5      < entry key = " porfiry "  value = " porfiry@gov.org " />   
6 </ util:map >   
7

使用改方法时,xml文件头需要使用:

1     xmlns:util = " http://www.springframework.org/schema/util "
2     xsi:schemaLocation = " http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
3                         http: // www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-2.5.xsd ">


引用:
http://macrochen.iteye.com/blog/392616
http://springindepth.com/book/in-depth-ioc-collections.html

相关文章
|
4月前
|
Java Spring 容器
Spring注解开发定义bean及纯注解开发模式
Spring注解开发定义bean及纯注解开发模式
35 0
|
3月前
|
XML Java 数据格式
Spring5源码(6)-Spring注入集合属性
Spring5源码(6)-Spring注入集合属性
14 0
|
8月前
|
XML Java 数据格式
spring中List,Set,Map集合的输出(详解)
spring中List,Set,Map集合的输出(详解)
90 0
|
4月前
|
IDE Java 开发工具
灵活配置 Spring 集合:List、Set、Map、Properties 详解
使用<property>标签的value属性配置原始数据类型和ref属性配置对象引用的方式来定义Bean配置文件。这两种情况都涉及将单一值传递给Bean
79 1
|
4月前
|
缓存 Java 关系型数据库
Spring Boot实现RESTful接口架构实战(包括REST的讲解、定义、REST服务测试)
Spring Boot实现RESTful接口架构实战(包括REST的讲解、定义、REST服务测试)
52 0
|
4月前
|
XML Java 数据格式
spring怎么去引用/注入集合/数组类型和 怎么通过 util 名称空间创建 list以及 怎么去通过级联属性赋值
spring怎么去引用/注入集合/数组类型和 怎么通过 util 名称空间创建 list以及 怎么去通过级联属性赋值
30 0
|
4月前
|
XML 前端开发 Java
【Spring 源码】 深入理解 Bean 定义之 BeanDefinition
【Spring 源码】 深入理解 Bean 定义之 BeanDefinition
|
6月前
|
Java 数据库连接 数据库
(mybatis与spring集合
(mybatis与spring集合
27 0
|
8月前
|
开发框架 安全 Java
为什么Spring中每个Bean都要定义作用域
前面的视频中都有提到过Spring Bean的作用域。本期视频呢,我针对Spring Bean作用域做一个详细的解答。关于Spring Bean的作用域,我一共分为两个部分来介绍。首先,介绍Spring Bean作用域的定义,然后,介绍Spring为什么要定义作用域?
50 0
|
8月前
|
Java Spring 容器
Spring Boot 定义系统启动任务,你会几种方式?
Spring Boot 定义系统启动任务,你会几种方式?