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,如需转载请自行联系原作者
相关文章
|
1天前
|
C++
【c++】map和set的封装
【c++】map和set的封装
5 0
|
1天前
|
存储 C++ 容器
【c++】set|map
【c++】set|map
4 0
|
1天前
|
存储 Python
Python中list, tuple, dict,set的区别和使用场景
Python中list, tuple, dict,set的区别和使用场景
|
2天前
|
存储 自然语言处理 C++
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
11 0
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
|
2天前
|
编译器 C++ 容器
通过红黑树封装 map 和 set 容器
通过红黑树封装 map 和 set 容器
|
2天前
|
存储 C++ 容器
【C++】学习笔记——map和set
【C++】学习笔记——map和set
7 0
|
2天前
|
Go
go语言map、实现set
go语言map、实现set
9 0
|
2天前
|
存储 消息中间件 算法
Java中的集合框架详解:List、Set、Map的使用场景
Java中的集合框架详解:List、Set、Map的使用场景
|
2天前
|
Dart
Dart之集合详解(List、Set、Map)
Dart之集合详解(List、Set、Map)
9 1
|
6天前
|
存储 算法 NoSQL
C++一分钟之-map与set容器详解
【6月更文挑战第21天】C++ STL的`map`和`set`是基于红黑树的关联容器,提供有序存储和高效查找。`map`存储键值对,键唯一,值可重复;`set`仅存储唯一键。两者操作时间复杂度为O(log n)。常见问题包括键的唯一性和迭代器稳定性。自定义比较函数可用于定制排序规则,内存管理需注意适时释放。理解和善用这些工具能提升代码效率。
12 3