1.基础类

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

2.配置文件

<?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>

3.调用

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

4.结果

Spring.NET学习笔记8——集合类型的注入(基础篇)的更多相关文章

  1. Spring.NET学习笔记7——依赖对象的注入(基础篇) Level 200

    1.person类 public class Person    {        public string Name { get; set; }        public int Age { g ...

  2. 【Spring实战】—— 7 复杂集合类型的注入

    之前讲解了Spring的基本类型和bean引用的注入,接下来学习一下复杂集合类型的注入,例如:List.Set.Map等. 对于程序员来说,掌握多种语言是基本的技能. 我们这里做了一个小例子,程序员们 ...

  3. Spring.Net学习笔记(5)-集合注入

    一.开发环境 系统:Win10 编译器:VS2013 .net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll 1.3.1 Common.Loggin.dll ...

  4. Spring.Net学习笔记(4)-属性及构造器注入

    一.开发环境 操作系统:Win10 编译器:VS2013 .Net版本:.net framework4.5 二.涉及程序集 Spring.Core.dll:1.3.1 Common.Logging.d ...

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

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

  6. 【转】Spring.NET学习笔记——目录

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  7. Spring.NET学习笔记——目录(原)

    目录 前言 Spring.NET学习笔记——前言 第一阶段:控制反转与依赖注入IoC&DI Spring.NET学习笔记1——控制反转(基础篇) Level 200 Spring.NET学习笔 ...

  8. Spring Boot 学习笔记(六) 整合 RESTful 参数传递

    Spring Boot 学习笔记 源码地址 Spring Boot 学习笔记(一) hello world Spring Boot 学习笔记(二) 整合 log4j2 Spring Boot 学习笔记 ...

  9. SpringBoot + Spring Security 学习笔记(三)实现图片验证码认证

    整体实现逻辑 前端在登录页面时,自动从后台获取最新的验证码图片 服务器接收获取生成验证码请求,生成验证码和对应的图片,图片响应回前端,验证码保存一份到服务器的 session 中 前端用户登录时携带当 ...

随机推荐

  1. 支持向量机(理论+opencv实现)

    从基础开始讲起,没有这些东西看支持向量机真的很难!   1.拉格朗日乘子(Lagrangemultiplier)   假设需要求极值的目标函数(objectivefunction)为f(x,y),限制 ...

  2. collections模块---(namedtuple、deque、OrderdDict、defaultdict、Counter)和configparser模块

    在内置数据类型(dict. list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter. deque.defaultdict.namedtuple 和 ...

  3. python 编码文件json.loads json.dumps

    import yaml d = {'name': '张三', 'age': '1'} print d jd = json.dumps(d, ensure_ascii=False, encoding=' ...

  4. 【独家】完美解决appium安装app时,需要手动确认安装的问题

    appium初始化driver时,如果未安装该app会先进行安装,安装时,很多安卓手机都会弹框,需要手动确认安装. 如小米的机器, 这是个头疼的问题,之前在网上找遍了,只有通过adb去点相对坐标成功了 ...

  5. linux时间

    1.查看时间:date Thu Mar  2 13:22:54 UTC 2017 2.查看时区:date -R Thu, 02 Mar 2017 13:23:32 +0000 3.Linux时间戳:d ...

  6. gevent 实现单线程下的socket链接

    通过gevent实现socket的多并发 server 端: import geventfrom gevent import socket, monkey monkey.patch_all() #进行 ...

  7. Simple2D-19(音乐播放器)播放器的源码实现

    使用 BASS 和 ImGui 实现音乐播放器 MusicPlayer. 将播放器和一个文件夹关联起来,程序刚开始运行的时候就从该文件夹加载所有音频文件.而文件夹的路径则保存在配置文件中,所以程序的第 ...

  8. UI5-文档-4.36-Device Adaptation

    现在,我们根据运行应用程序的设备配置控件的可见性和属性.通过使用sap.ui.设备API和定义一个设备模型,我们将使应用程序在许多设备上看起来很棒. Preview On phone devices, ...

  9. WP 8.1 status bar

    A status bar is the bar showing signal, battery and time on the top of the phone's screen. In WP8.1 ...

  10. 进入一个docker容器

    Starting from Docker 1.3 you can use Docker exec to enter a Docker container : docker exec -it CONTA ...