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,如需转载请自行联系原作者
相关文章
|
6天前
|
存储 缓存 JavaScript
Set和Map有什么区别?
Set和Map有什么区别?
19 1
|
4月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
115 2
|
6天前
|
存储 缓存 JavaScript
在JavaScript中,Set和Map的性能有什么区别?
在JavaScript中,Set和Map的性能有什么区别?
29 0
|
1月前
|
存储 JavaScript 前端开发
for...of循环在遍历Set和Map时的注意事项有哪些?
for...of循环在遍历Set和Map时的注意事项有哪些?
57 0
|
1月前
|
存储 C++ 容器
unordered_set、unordered_multiset、unordered_map、unordered_multimap的介绍及使用
unordered_set是不按特定顺序存储键值的关联式容器,其允许通过键值快速的索引到对应的元素。在unordered_set中,元素的值同时也是唯一地标识它的key。在内部,unordered_set中的元素没有按照任何特定的顺序排序,为了能在常数范围内找到指定的key,unordered_set将相同哈希值的键值放在相同的桶中。unordered_set容器通过key访问单个元素要比set快,但它通常在遍历元素子集的范围迭代方面效率较低。它的迭代器至少是前向迭代器。前向迭代器的特性。
76 0
|
1月前
|
编译器 C++ 容器
用一棵红黑树同时封装出map和set
再完成上面的代码后,我们的底层代码已经完成了,这时候已经是一个底层STL的红黑树了,已经已符合库里面的要求了,这时候我们是需要给他穿上对应的“衣服”,比如穿上set的“衣服”,那么这个穿上set的“衣服”,那么他就符合库里面set的要求了,同样map一样,这时候我们就需要实现set与map了。因此,上层容器map需要向底层红黑树提供一个仿函数,用于获取T当中的键值Key,这样一来,当底层红黑树当中需要比较两个结点的键值时,就可以通过这个仿函数来获取T当中的键值了。我们就可以使用仿函数了。
30 0
|
1月前
|
存储 编译器 容器
set、map、multiset、multimap的介绍及使用以及区别,注意事项
set是按照一定次序存储元素的容器,使用set的迭代器遍历set中的元素,可以得到有序序列。set当中存储元素的value都是唯一的,不可以重复,因此可以使用set进行去重。set默认是升序的,但是其内部默认不是按照大于比较,而是按照小于比较。set中的元素不能被修改,因为set在底层是用二叉搜索树来实现的,若是对二叉搜索树当中某个结点的值进行了修改,那么这棵树将不再是二叉搜索树。
79 0
|
5月前
|
编译器 容器
哈希表模拟封装unordered_map和unordered_set
哈希表模拟封装unordered_map和unordered_set
|
5月前
|
编译器 测试技术 计算机视觉
红黑树模拟封装map和set
红黑树模拟封装map和set
|
6月前
|
自然语言处理 IDE Java
SpringBoot start.aliyun.com创建项目,解决properties乱码的问题
通过确保文件和开发环境的编码一致,配置 Maven 编码,设置 Spring Boot 应用和嵌入式服务器的编码,可以有效解决 properties 文件的乱码问题。以上步骤可以帮助开发者确保在 Spring Boot 项目中正确处理和显示多语言字符,避免因编码问题导致的乱码现象。
142 5