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

一、开发环境

系统:Win10

编译器:VS2013

.net版本:.net framework4.5

二、涉及程序集

Spring.Core.dll 1.3.1

Common.Loggin.dll

三、开发过程

1.项目结构

image

2.编写Person.cs

namespace SpringNetSetDi
{
    public class Person
    {
        public string RealName { get; set; }
        public string NickName { get; set; }
        public string LoginName { get; set; }
        public string ShowName { get; set; }
    }
}

3.编写Animal.cs

namespace SpringNetSetDi
{
    public class Animal
    {
        public string Name { get; set; }
        public IList TypeList { get; set; }
    }
}

4.编写Zoo.cs

namespace SpringNetSetDi
{
    public class Zoo
    {
        public List<Animal> AnimalList { get; set; }
    }
}

5.编写WebSetting.cs

namespace SpringNetSetDi
{
    public class WebSetting
    {
        public IDictionary<int, string> PrintSetting { get; set; }
    }
}

6.配置文件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"></resource>
    </context>
    <objects  xmlns="http://www.springframework.net">
      <!--01设置空值-->
      <object name="person" type="SpringNetSetDi.Person,SpringNetSetDi">
        <property name="ShowName" value="Kimisme"></property>
        <property name="RealName" value=""></property>
        <property name="NickName">
          <value></value>
        </property>
        <property name="LoginName">
          <null/>
        </property>
      </object>
      <!--02IList类型注入-->
      <object name="animal" type="SpringNetSetDi.Animal,SpringNetSetDi">
        <property name="TypeList">
          <list element-type="string">
            <value>哺乳类</value>
            <value>鸟类</value>
            <value>爬行类</value>
            <value>昆虫类</value>
            <value>两栖类</value>
          </list>
        </property>
      </object>
      <!--03泛型List注入-->
      <object name="zoo" type="SpringNetSetDi.Zoo,SpringNetSetDi">
        <property name="AnimalList">
          <list element-type="SpringNetSetDI.Animal,SpringNetSetDi">
            <object type="SpringNetSetDi.Animal,SpringNetSetDi">
              <property name="Name" value="Eagle"></property>
              <property name="TypeList">
                <list element-type="string">
                  <value>鸟类</value>
                </list>
              </property>
            </object>
          </list>
        </property>
      </object>
      <!--04Dictionary注入-->
      <object name="webSetting" type="SpringNetSetDi.WebSetting,SpringNetSetDi">
        <property name="PrintSetting">
          <dictionary key-type="int" value-type="string">
            <entry key="1" value="默认打印"></entry>
            <entry key="2" value="个性打印"></entry>
          </dictionary>
        </property>
      </object>
    </objects>
  </spring>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

7.控制台代码

namespace SpringNetSetDi
{
    class Program
    {
        static void Main(string[] args)
        {
            IApplicationContext context = ContextRegistry.GetContext();
            Person person = context.GetObject("person") as Person;

            Animal animal = context.GetObject("animal") as Animal;

            Zoo zoo = context.GetObject("zoo") as Zoo;
            Console.ReadKey();
        }
    }
}

四、说明

这边有个坑,花了我好长时间,具体解决看 问题汇总

原文地址:https://www.cnblogs.com/kimisme/p/5343236.html