Spring.Net学习笔记七(集合类型的注入)
本文内容纲要:Spring.Net学习笔记七(集合类型的注入)
Spring.NET还支持集合类型的注入。而且使用起来也比较方便。
一、ILIst类型
使用
在元素中设置 element-type 属性表示泛型T的类型,例如 element-type="int" ,代表int型。
二、IDictionary类型
使用
同理,
完整代码如下:
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
本文内容总结:Spring.Net学习笔记七(集合类型的注入)
原文链接:https://www.cnblogs.com/wangyhua/p/IOC7.html
以上是 Spring.Net学习笔记七(集合类型的注入) 的全部内容, 来源链接: utcz.com/z/296023.html