码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-03-31 23:37:03      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:

一、开发环境

系统:Win10

编译器:VS2013

.net版本:.net framework4.5

二、涉及程序集

Spring.Core.dll 1.3.1

Common.Loggin.dll

三、开发过程

1.项目结构

技术分享

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

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

四、说明

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

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

标签:

原文地址:http://www.cnblogs.com/2star/p/5343236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!