[Spring.NET IoC] 之一:基本信息
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" 嘗試一下。
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);
}
}
}
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);
}
}
}
全站熱搜