Spring中注入List,Set,Map,Properties

简介:

下面的例子展示了如何注入

  • List – <list/>
  • Set – <set/>
  • Map – <map/>
  • Properties – <props/>

Spring beans

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import  java.util.List;
import  java.util.Map;
import  java.util.Properties;
import  java.util.Set;
  
public  class  Customer
{
     private  List<Object> lists;
     private  Set<Object> sets;
     private  Map<Object, Object> maps;
     private  Properties pros;
  
     //...
}

  配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
< 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-2.5.xsd">
  
     < bean  id="CustomerBean" class="com.mkyong.common.Customer">
  
         <!-- java.util.List -->
         < property  name="lists">
             < list >
                 < value >1</ value >
                 < ref  bean="PersonBean" />
                 < bean  class="com.mkyong.common.Person">
                     < property  name="name" value="mkyongList" />
                     < property  name="address" value="address" />
                     < property  name="age" value="28" />
                 </ bean >
             </ list >
         </ property >
  
         <!-- java.util.Set -->
         < property  name="sets">
             < set >
                 < value >1</ value >
                 < ref  bean="PersonBean" />
                 < bean  class="com.mkyong.common.Person">
                     < property  name="name" value="mkyongSet" />
                     < property  name="address" value="address" />
                     < property  name="age" value="28" />
                 </ bean >
             </ set >
         </ property >
  
         <!-- java.util.Map -->
         < property  name="maps">
             < map >
                 < entry  key="Key 1" value="1" />
                 < entry  key="Key 2" value-ref="PersonBean" />
                 < entry  key="Key 3">
                     < bean  class="com.mkyong.common.Person">
                         < property  name="name" value="mkyongMap" />
                         < property  name="address" value="address" />
                         < property  name="age" value="28" />
                     </ bean >
                 </ entry >
             </ map >
         </ property >
  
         <!-- java.util.Properties -->
         < property  name="pros">
             < props >
                 < prop  key="admin">admin@nospam.com</ prop >
                 < prop  key="support">support@nospam.com</ prop >
             </ props >
         </ property >
  
     </ bean >
  
     < bean  id="PersonBean" class="com.mkyong.common.Person">
         < property  name="name" value="mkyong1" />
         < property  name="address" value="address 1" />
         < property  name="age" value="28" />
     </ bean >
  
</ beans >

运行:

1
2
3
4
5
6
7
8
9
10
11
public  class  App
{
     public  static  void  main( String[] args )
     {
         ApplicationContext context =  new  ClassPathXmlApplicationContext( "SpringBeans.xml" );
  
         Customer cust = (Customer)context.getBean( "CustomerBean" );
         System.out.println(cust);
  
     }
}

  我们也可以使用ListFactoryBean。The ‘ListFactoryBean‘ class provides developer a way to create a concrete List collection class (ArrayList and LinkedList) in Spring’s bean configuration file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
< bean  id="CustomerBean" class="com.mkyong.common.Customer">
         < property  name="lists">
             < bean  class="org.springframework.beans.factory.config.ListFactoryBean">
                 < property  name="targetListClass">
                     < value >java.util.ArrayList</ value >
                 </ property >
                 < property  name="sourceList">
                     < list >
                         < value >1</ value >
                         < value >2</ value >
                         < value >3</ value >
                     </ list >
                 </ property >
             </ bean >
         </ property >
     </ bean >

  或者:加入:xmlns:util="http://www.springframework.org/schema/util"然后就可以:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< beans  xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:util="http://www.springframework.org/schema/util"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
     http://www.springframework.org/schema/util
     http://www.springframework.org/schema/util/spring-util-2.5.xsd">
  
     < bean  id="CustomerBean" class="com.mkyong.common.Customer">
         < property  name="lists">
             < util:list  list-class="java.util.ArrayList">
                 < value >1</ value >
                 < value >2</ value >
                 < value >3</ value >
             </ util:list >
         </ property >
     </ bean >
  
</ beans >

 

         SetFactoryBean The ‘SetFactoryBean‘ class provides developer a way to create a concrete Set collection (HashSet and TreeSet) in Spring’s bean configuration file.

set和上面的一样:

1
2
3
4
5
6
7
8
9
< bean  id="CustomerBean" class="com.mkyong.common.Customer">
         < property  name="sets">
             < util:set  set-class="java.util.HashSet">
                 < value >1</ value >
                 < value >2</ value >
                 < value >3</ value >
             </ util:set >
         </ property >
     </ bean >

         MapFactoryBean The ‘MapFactoryBean‘ class provides developer a way to create a concrete Map collection class (HashMap and TreeMap) in Spring’s bean configuration file.

map也一样:

1
2
3
4
5
6
7
8
9
< bean  id="CustomerBean" class="com.mkyong.common.Customer">
         < property  name="maps">
             < util:map  map-class="java.util.HashMap">
                 < entry  key="Key1" value="1" />
                 < entry  key="Key2" value="2" />
                 < entry  key="Key3" value="3" />
             </ util:map >
         </ property >
     </ bean >

  

 

 

 

 


==============================================================================
本文转自被遗忘的博客园博客,原文链接:http://www.cnblogs.com/rollenholt/archive/2012/12/27/2835122.html,如需转载请自行联系原作者
相关文章
|
2月前
|
Java
【Java集合类面试二十三】、List和Set有什么区别?
List和Set的主要区别在于List是一个有序且允许元素重复的集合,而Set是一个无序且元素不重复的集合。
|
15天前
|
存储 Java API
【数据结构】map&set详解
本文详细介绍了Java集合框架中的Set系列和Map系列集合。Set系列包括HashSet(哈希表实现,无序且元素唯一)、LinkedHashSet(保持插入顺序的HashSet)、TreeSet(红黑树实现,自动排序)。Map系列为双列集合,键值一一对应,键不可重复,值可重复。文章还介绍了HashMap、LinkedHashMap、TreeMap的具体实现与应用场景,并提供了面试题示例,如随机链表复制、宝石与石头、前K个高频单词等问题的解决方案。
22 6
【数据结构】map&set详解
|
4天前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
15 5
|
7天前
|
存储 JavaScript 前端开发
js的map和set |21
js的map和set |21
|
6天前
|
存储 前端开发 API
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
该文章详细介绍了ES6中Set和Map数据结构的特性和使用方法,并探讨了它们在前端开发中的具体应用,包括如何利用这些数据结构来解决常见的编程问题。
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
|
2月前
|
存储 安全 Java
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set
|
2月前
|
存储 NoSQL 算法
Redis6入门到实战------ 三、常用五大数据类型(列表(List)、集合(Set)、哈希(Hash)、Zset(sorted set))
这是关于Redis 6入门到实战的文章,具体内容涉及Redis的五大数据类型:列表(List)、集合(Set)、哈希(Hash)、有序集合(Zset(sorted set))。文章详细介绍了这些数据类型的特点、常用命令以及它们背后的数据结构。如果您有任何关于Redis的具体问题或需要进一步的帮助,请随时告诉我。
|
2月前
|
Java
【Java集合类面试二十二】、Map和Set有什么区别?
该CSDN博客文章讨论了Map和Set的区别,但提供的内容摘要并未直接解释这两种集合类型的差异。通常,Map是一种键值对集合,提供通过键快速检索值的能力,而Set是一个不允许重复元素的集合。
|
2月前
|
存储 Java 索引
|
2月前
|
测试技术 索引 Python
Python接口自动化测试框架(基础篇)-- 常用数据类型list&set()
本文介绍了Python中list和set两种数据类型的使用,包括它们的创建、取值、增删改查操作、排序以及内置函数的使用,还探讨了list的比较函数和set的快速去重功能。
20 0
下一篇
无影云桌面