您的位置:知识库 » 软件测试

[Spring.NET IoC] 之一:基本信息

作者: Q.yuhen  来源: Q.yuhen  发布时间: 2008-08-17 23:03  阅读: 4051 次  推荐: 0   原文链接   [收藏]  
Spring.NET 移植自著名的 Java 开源项目 —— Spring,借助于 .NET 强大的反射机制,甚至拥有比原 Java 版本更强大的功能。只是不知道什么原因,在 .NET 领域似乎没有多少热度,其影响力甚至不如 Castle。因准备在个人项目中使用 IoC,因此花些时间对 Spring.NET 和 Castle 都作一些了解,本文权作学习笔记。

Spring.NET 的宣传口号中有 "non-invasiveness. Quite simply" 的字样,表示是非入侵且易用的,当然作为一个IoC容器,这些都是最基本的特征。

在 Spring.NET IoC 中最核心的内容应该是 IObjectFactory、IApplicationContext、IObjectDefinition 这三个接口了。IObjectFactory 是核心容器接口,负责管理容器内的注入对象,而 IApplicationContext 则是 IObjectFactory 的继承,它扩展了一些功能。IObjectDefinition 是注入对象的定义接口,供 IObjectFactory / IApplicationContext 调用。

大多数时候我们会选择 IApplicationContext 接口的实现类来操控 Spring.NET IoC 容器。

1. Spring.Context.Support.ContextRegistry
将配置信息写入 app.config / web.config 时所使用该类。

2. Spring.Context.Support.XmlApplicationContext
使用独立的 xml 配置文件,或者使用多个 xml 配置文件时,使用该类。

3. Spring.Context.Support.StaticApplicationContext
直接在代码中装配容器时,使用该类。

当然,你还可以使用 Spring.Objects.Factory.Xml.XmlObjectFactory 等直接实现 IObjectFactory 的类型。

作为该篇的结束,写一个简单的 "Hello World" 尝试一下。
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using Spring.Core;
using Spring.Context;
using Spring.Context.Support;

namespace ConsoleApplication1.SpringNet
{
  public class HelloWorld
  {
    public override string ToString()
    {
      return "Hello, World!";
    }
  }

  public class Program
  {
    static void Main(string[] args)
    {
      StaticApplicationContext context = new StaticApplicationContext();
      context.RegisterPrototype("HelloWorld", typeof(HelloWorld), null);

      object o = context.GetObject("HelloWorld");
      Console.WriteLine(o);
    }
  }
}
0
0
 

软件测试热门文章

    软件测试最新文章

      最新新闻

        热门新闻