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
    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();
        }
    }

  输入结果如下:

代码下载

转载于:https://www.cnblogs.com/yunchun86/archive/2009/12/10/1621455.html

Spring.NET学习笔记8——集合类型的注入(基础篇) Level 200相关推荐

  1. Spring.NET学习笔记10——方法的注入(基础篇) Level 200

    多数用户都会将容器中的大部分对象布署为singleton模式.当一个singleton对象需要和另一个singleton对象协作,或者一个非singleton对象需要和另一个非singleson对象协 ...

  2. Spring框架中提取list集合类型属性注入

    提取list集合类型属性注入 前言 引入名称空间 编写`xml`配置文件 运行结果 前言 对于某一个类型属性通用性较高的情况下,可以单独的提取出来,给需要的bean进行引用. 有关类的创建见<S ...

  3. Spring.NET学习笔记15——AOP的配置(基础篇) Level 200

    上篇我学习了Spring.NET的四种通知类型,AOP的实现方案比较复杂,是通过代码实现的.而Spring.NET框架给我们提供了配置的方式来实现AOP的功能.到目前为止,我们已经讨论过使用Proxy ...

  4. Python3《机器学习实战》学习笔记(二):决策树基础篇之让我们从相亲说起

    转载请注明作者和出处: http://blog.csdn.net/c406495762 运行平台: Windows Python版本: Python3.x IDE: Sublime text3 个人网 ...

  5. web安全学习笔记--sql语句(sql注入基础上)

    一.基础知和表内操作语法 1.sql语句对大小写不敏感!!! SELECT - 从数据库表中获取数据:select * from (columns/tables/databases); UPDATE ...

  6. Spring.NET学习笔记13——AOP的概念(基础篇) Level 200

    上篇我们简单的了解了AOP的应用场景,知道AOP编程的重要性.这篇我们先看一段代码,来开始今天的学习. 回顾与上篇类似的代码:SecurityService类的IsPass判断用户名为"ad ...

  7. Spring.NET学习笔记1——控制反转(基础篇) Level 200

    在学习Spring.NET这个控制反转(IoC)和面向切面(AOP)的容器框架之前,我们先来看一下什么是控制反转(IoC). 控制反转(Inversion of Control,英文缩写为IoC),也 ...

  8. Spring.NET学习笔记11——自定义对象行为(基础篇) Level 200

    Spring.NET通过几个专门的接口来控制容器中对象的行为.说到对象的行为无非就要提到对象的生命周期控制.类似在WinForm开发,Form生命周期中,Load方法为Form的载入方法和Dispos ...

  9. python学习笔记六 初识面向对象上(基础篇)

    python面向对象 面向对象编程(Object-Oriented Programming )介绍 对于编程语言的初学者来讲,OOP不是一个很容易理解的编程方式,虽然大家都知道OOP的三大特性是继承. ...

最新文章

  1. Python热文Top10,精选自1000篇文章
  2. BOOST_PREDEF_WORKAROUND宏相关的测试程序
  3. Linux 权限管理: 权限的概念、权限管理、文件访问权限的设置、 粘滞位
  4. java中怎么表示数组中的某个值_简易Java(12):如何高效检查一个数组中是否包含某个值?...
  5. php文件显示不完整,github文件显示不全
  6. java mysql 回滚_Java 中对数据库操作时的 回滚
  7. SignalR 跨域解决方案全面
  8. 数据之路 - Python爬虫 - BeautifulSoup库
  9. 反向题在测试问卷信效度_[问卷的信度和效度分析]问卷信度和效度分析
  10. debian 网络代理_Debian系统代理服务器安装及配置
  11. 【CGAL】提取中心线
  12. 女子人身安全防卫指南
  13. js:进制转换、保留指定位数小数、RGB/Hex颜色色值转换
  14. 用python玩转数据作业答案_大学mooc2020年用Python玩转数据作业答案
  15. Java基础语法总复习
  16. 爬虫(四十六)通用标准库 queue(三十七)
  17. 马斯克裁员推特,对互联网行业的影响……
  18. 读《读大学,究竟读什么》感悟一
  19. 武汉理工大学数据结构综合实验——二叉树与赫夫曼图片压缩
  20. NX二次开发(C#)-建模-参数化模型自动修改

热门文章

  1. c语言生成随机英文字母,菜鸟求助,写一个随机输出26个英文字母的程序
  2. elasticsearch-5.6.12 单点安装包括 HEAD插件安装
  3. pythonreplace回调函数,python回调函数返回非
  4. mysql前一天_mysql查询当天,前一天,一周,一个月
  5. 2018上半年信息安全工程师真题含答案(下午题)
  6. cocos2d-x的未来之旅
  7. linux 文件指针,Linux中文件描述符fd与文件指针FILE*互相转换实例解析
  8. 主板没有rgb接口怎么接灯_性价比稳定的RGB水冷散热器:乔思伯天使眼TW2-240测评...
  9. div 重新加载_JS之 加载模糊文本动画
  10. BootstrapTable冻结表头(一)