首页 > 编程语言 > 详细

Spring.NET学习笔记8——集合类型的注入(基础篇)

时间:2015-03-23 15:08:51      阅读:194      评论:0      收藏:0      [点我收藏+]

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——集合类型的注入(基础篇)

原文:http://www.cnblogs.com/kexb/p/4359766.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!