Spring.NET教程(七)——集合类型的注入(基础篇)
2009-12-30 13:40
453 查看
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"> 。
完整代码如下:
Domain
App.config
Program
输入结果如下:
一、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"> 。
完整代码如下:
Domain
public class Happy { public override string ToString() { return "每天都开心,每天都有好心情"; } } public class OneYear { public override string ToString() { return "快乐的一年"; } } public class Person { public IList<Person> BestFriends { get; set; } public IList HappyYears { get; set; } public IList<int> Years { get; set; } public IDictionary HappyDic { get; set; } public IDictionary<string,object> HappyTimes { get; set; } }
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" /> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> </sectionGroup> </configSections> <spring> <context> <resource uri="config://spring/objects" /> </context> <objects xmlns="http://www.springframework.net"> <object id="person" type="SpringNetDi.Person, SpringNetDi"> <!--空集合属性--> <property name="BestFriends"> <null/> </property> <!--System.Collections.IList注入 --> <property name="HappyYears"> <list> <value>1992</value> <value>1998 年</value> <ref object="oneYear"/> </list> </property> <!--System.Collections.IList<int>注入 --> <property name="Years"> <list element-type="int"> <value>1992</value> <value>1998</value> <value>2000</value> </list> </property> <!--System.Collections.IDictionary注入--> <property name="HappyDic"> <dictionary key-type="string" value-type="object"> <entry key="第一开心" value="每天都能睡一个好觉"/> <entry key="第二开心" value-ref="happy"/> </dictionary> </property> <!--System.Collections.IDictionary<object,object>注入--> <property name="HappyTimes"> <dictionary key-type="string" value-type="object"> <entry key="第一开心" value="每天都能睡一个好觉"/> <entry key="第二开心" value-ref="happy"/> </dictionary> </property> </object> <object id="oneYear" type="SpringNetDi.OneYear,SpringNetDi"/> <object id="happy" type="SpringNetDi.Happy,SpringNetDi"/> </objects> </spring> </configuration>
Program
class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); Person person = ctx.GetObject("person") as Person; Console.WriteLine("空值"); string bestFriend = person.BestFriends == null ? "我的朋友太多了" : "我只有一个好朋友"; Console.WriteLine(bestFriend); Console.WriteLine(); Console.WriteLine("IList"); foreach (var item in person.HappyYears) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("泛型Ilist<int>"); foreach (int item in person.Years) { Console.WriteLine(item); } Console.WriteLine(); Console.WriteLine("IDictionary"); foreach (DictionaryEntry item in person.HappyDic) { Console.WriteLine(item.Key + " 是 " + item.Value); } Console.WriteLine(); Console.WriteLine("泛型IDictionary<string,object>"); foreach (KeyValuePair<string,object> item in person.HappyTimes) { Console.WriteLine(item.Key + " 是 " + item.Value); } Console.ReadLine(); } }
输入结果如下:
相关文章推荐
- Spring.NET教程(十二)——AOP的通知类型(基础篇)
- Spring.NET教程(六)——依赖对象的注入(基础篇)
- Spring.NET教程(八)——方法的注入(基础篇)
- Spring.NET教程(四)——容器中对象的作用域(基础篇)
- spring利用set操作对引用类型和集合类型进行注入值
- Spring、基本类型属性和集合类型属性的注入
- Spring集合类型的bean及其注入方法
- spring集合类型注入
- Spring.NET学习笔记8——集合类型的注入(基础篇)
- Spring.NET学习笔记8——集合类型的注入(基础篇) Level 200
- Spring.NET教程(十五)——事务传播行为(基础篇)
- spring对集合类型进行注入
- Spring对集合类型的属性注入
- 【Spring实战】—— 7 复杂集合类型的注入
- Spring、基本类型属性和集合类型属性的注入
- Spring.NET教程—环境搭建(基础篇)
- Spring-注入参数详解-[集合类型属性]
- Spring-注入参数详解-[通过util命名空间简化集合类型的配置]
- Spring.NET学习笔记8——集合类型的注入(基础篇)
- spring集合类型的setter注入的一个简单例子