Spring.Net学习笔记七(集合类型的注入)

Spring.NET还支持集合类型的注入。而且使用起来也比较方便。

  一、ILIst类型
  使用<list>元素作为ILIst的标签,value为集合中元素的值。也可以注入对象,甚至关联其它对象,使用 <ref/>元素表示关联的对象,object 属性为所关联对象的id或name。集合可以为空,用<null/>元素来标记。

  在<list>元素中设置 element-type 属性表示泛型T的类型,例如 element-type="int"  ,代表int型。

  

 

  二、IDictionary类型

  使用<dictionary>元素来表示IDictionary接口的实现类型。<entry/>表示IDictionary集合的元素。key和value属性为元素的键值队,value-ref为关联的元素。

  同理,<dictionary>元素的key-type和value-type属性来表示泛型IDictionary,例如 <dictionary key-type="string" value-type="object"> 。

 

  完整代码如下:

   

 1 namespace Dao.IOC
 2 {
 3     public class Happy
 4     {
 5         public override string ToString()
 6         {
 7             return "每天都开心,每天都有好心情";
 8         }
 9     }
10     public class OneYear
11     {
12         public override string ToString()
13         {
14             return "快乐的一年";
15         }
16     }
17     public class Person
18     {
19         public IList<Person> BestFriends { get; set; }
20         public IList HappyYears { get; set; }
21         public IList<int> Years { get; set; }
22         public IDictionary HappyDic { get; set; }
23         public IDictionary<string, object> HappyTimes { get; set; }
24     }
25 }
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <configuration>
 3 
 4   <configSections>
 5     <sectionGroup name="spring">
 6       <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
 7       <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
 8     </sectionGroup>
 9   </configSections>
10 
11   <spring>
12 
13     <context>
14       <resource uri="config://spring/objects" />
15     </context>
16 
17     <objects xmlns="http://www.springframework.net">
18 
19       <object id="person" type="Dao.IOC.Person, Dao">
20 
21 
22         <!--空集合属性-->
23         <property name="BestFriends">
24           <null/>
25         </property>
26 
27         <!--System.Collections.IList注入 -->
28         <property name="HappyYears">
29           <list>
30             <value>1992</value>
31             <value>1998 年</value>
32             <ref object="oneYear"/>
33           </list>
34         </property>
35 
36         <!--System.Collections.IList<int>注入 -->
37         <property name="Years">
38           <list element-type="int">
39             <value>1992</value>
40             <value>1998</value>
41             <value>2000</value>
42           </list>
43         </property>
44 
45         <!--System.Collections.IDictionary注入-->
46         <property name="HappyDic">
47           <dictionary key-type="string" value-type="object">
48             <entry key="第一开心" value="每天都能睡一个好觉"/>
49             <entry key="第二开心" value-ref="happy"/>
50           </dictionary>
51         </property>
52 
53         <!--System.Collections.IDictionary<object,object>注入-->
54         <property name="HappyTimes">
55           <dictionary key-type="string" value-type="object">
56             <entry key="第一开心" value="每天都能睡一个好觉"/>
57             <entry key="第二开心" value-ref="happy"/>
58           </dictionary>
59         </property>
60 
61       </object>
62 
63       <object id="oneYear" type="Dao.IOC.OneYear,Dao"/>
64 
65       <object id="happy" type="Dao.IOC.Happy,Dao"/>
66 
67     </objects>
68 
69   </spring>
70 
71 </configuration>
 1  static void Main(string[] args)
 2         {
 3 
 4              IApplicationContext ctx = ContextRegistry.GetContext();
 5 
 6             Person person = ctx.GetObject("person") as Person;
 7 
 8             Console.WriteLine("空值");
 9             string bestFriend = person.BestFriends == null ? "我的朋友太多了" : "我只有一个好朋友";
10             Console.WriteLine(bestFriend);
11             Console.WriteLine();
12 
13             Console.WriteLine("IList");
14             foreach (var item in person.HappyYears)
15             {
16                 Console.WriteLine(item);
17             }
18             Console.WriteLine();
19 
20             Console.WriteLine("泛型Ilist<int>");
21             foreach (int item in person.Years)
22             {
23                 Console.WriteLine(item);
24             }
25             Console.WriteLine();
26 
27             Console.WriteLine("IDictionary");
28             foreach (DictionaryEntry item in person.HappyDic)
29             {
30                 Console.WriteLine(item.Key + "" + item.Value);
31             }
32             Console.WriteLine();
33 
34             Console.WriteLine("泛型IDictionary<string,object>");
35             foreach (KeyValuePair<string, object> item in person.HappyTimes)
36             {
37                 Console.WriteLine(item.Key + "" + item.Value);
38             }
39 
40             Console.ReadLine();
41         }

   博客原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/02/SpringNet_List.html

posted @ 2015-08-06 14:27  王永华  阅读(275)  评论(0编辑  收藏  举报