Spring.NET依賴註入框架學習--簡單對象註入
Spring.NET依賴註入框架學習--簡單對象註入
在前面的倆篇中講解了依賴註入的概念以及Spring.NET框架的核心模塊介紹,今天就要看看怎麽來使用Spring.NET實現一個簡單的對象註入
常用文件
我們使用Spring.Net 框架經常要使用到的是以下文件:
- Common.Logging.dll 包含了Spring.Net日誌方面的功能(必須)
- Spring.Core.dll 包含了Spring.Net 的核心庫(必須)
- Spring.Data.dll 包含了Spring.Net 的數據訪問功能
- Spring.Aop. dll 包含了Spring.Net 對面向切面編程(AOP)的支持
- Spring.Web.dll 包含了Spring.Net 對ASP.NET進行了一系列功能擴展
Spring.NET常用接口
IObjectFactory和IApplicationContext
IObjectFactory接口提供了一種高級配置機制,可以管理任何類型的對象,提供了配置框架和基本功能。
IApplicationContext是IObjectFactory的子接口,表示Spring IoC容器,負責在應用程序中實例化,配置和組裝許多對象。容器通過讀取配置元數據獲取對象的實例化,配置和組裝。配置元數據以XML表示。它增加了與Spring.NET面向方面編程(AOP)功能,消息資源處理(用於國際化),事件傳播和Web應用程序上下文(如Web應用程序上下文)在Web應用程序中使用的更容易的集成,IApplicationContext增加了更多的企業特定功能。 IApplicationContext是IObjectFactory的一個完整的超集。
Spring.NET提供了幾個IApplicationContext接口的實現。在獨立的應用程序中,通常在應用程序的App.config文件中以編程方式或聲明方式創建XmlApplicationContext的實例。
在Web應用程序中,Spring提供了一個WebApplicationContext實現,該實現通過在Web.config文件中添加自定義HTTP模塊和HTTP處理程序來配置
下圖是Spring.NET的工作原理圖
如上圖所示,Spring.NET IoC容器使用一種形式的配置元數據;這個配置元數據表示作為應用程序開發人員如何告訴Spring容器在應用程序中實例化,配置和組裝對象。配置元數據以簡單直觀的XML格式提供
Spring.Net 的配置介紹
Spring.Net 的配置文件(一般情況下加入到App.Config或者Web.Config中)內容如下都是XML文件格式
配置文件中必須要要具有的節點
spring結點
spring結點 方式一
<spring> <context type="Spring.Context.Support.XmlApplicationContext, Spring.Core"> <resource uri="file://objects.xml"/> <resource uri="assembly://MyAssembly/MyProject/objects-dal-layer.xml"/> </context> </spring>
spring結點 方式二
<spring> <context> <resource uri="file://objects.xml"/> <resource uri="assembly://MyAssembly/MyProject/objects-dal-layer.xml"/> </context> </spring>
<context>節點的type屬性是可選的,在Windows應用中,其默認值就是Spring.Context.Support.XmlApplicationContext,所以方式一和方式二完全相同
spring和context節點的名稱不是任意的,必須是"spring"和"context",Spring.NET本身將"spring/context"作為字符串常量定義在了AbstractApplicationContext類中以表示上下文的節點名稱所以這個結點名稱是不能隨便改的,若要引用由以上配置創建的容器,可使用下面的代碼:
IApplicationContext ctx = ContextRegistry.GetContext();
ContextRegistry類既可用來初始化應用程序上下文,也可用來以服務定位器風格對容器中的對象進行訪問,註意,使這一切成為可能的是Spring.Context.Support.ContextHandler類,該類實現了FCL的IConfigurationSectionHandler接口。必須在.NET配置文件的<configSections>節點中註冊這個類,註冊了這個節點處理器後,配置文件中的<spring>節點才能起作用,如下所示:
configSections結點
<configSections> <sectionGroup name="spring"> <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> </sectionGroup> </configSections>
源數據對象xml文件格式
<objects xmlns="http://www.springframework.net"> <object id="..." type="..."> <!-- collaborators and configuration for this object go here --> </object> <object id="...." type="..."> <!-- collaborators and configuration for this object go here --> </object> <!-- more object definitions go here --> </objects>
id屬性是用來標識單個對象定義的字符串。 type屬性定義對象的類型,並使用完全限定的類型名稱,包括程序集名稱。 id屬性的值是指協作對象。
實例介紹
Person.cs
/***************************************************** * ProjectName: Spring.NET01 * Description: * ClassName: Person * CLRVersion: 4.0.30319.42000 * Author: JiYF * NameSpace: Spring.NET01 * MachineName: JIYF_PC * CreateTime: 2018/1/14 11:59:12 * UpdatedTime: 2018/1/14 11:59:12 *****************************************************/ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Spring.NET01 { public class Person { public Person() { } ~Person() { } public void print() { Console.WriteLine("我是一個Person對象"); } } }
根據配置文件格式我們配置一個Person對象
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"> <!--一個person對象--> <object id="person1" type="Spring.NET01.Person,Spring.NET01"> </object> </objects> </spring> </configuration>
通過Sprint.NET容器註入對象
第一步引用Spring.NET動態庫文件,這裏只需要引用Spring.Core.dll 和Common.Logging.dll動態庫文件即可
代碼測試:
class Program { static void Main(string[] args) { //普通對象創建 Person person = new Person(); person.print(); //通過Spring.NET ioc註入對象 IApplicationContext ctx = ContextRegistry.GetContext(); Person bennanhai = (Person)ctx.GetObject("person1"); bennanhai.print(); Console.Read(); } }
執行結果:
這樣一個簡單的依賴註入例子就實現了
源代碼工程下載
Spring.NET依賴註入框架學習--簡單對象註入