配置 Spring.NET
生活随笔
收集整理的這篇文章主要介紹了
配置 Spring.NET
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作為一個容器,當然首先要存在一個容器對象了。Spring.NET 中的容器定義在程序集 Spring.Core 中,直接添加這個程序集的引用就可以開始使用了。這個程序集位于 Spring.NET-1.3.1\Spring.NET\bin\net\4.0\release 中。
在 Spring.NET 中,對于通過編程方式使用容器的環境,提供了 Spring.Context.Support.StaticApplicationContext,我們可以直接創建這個容器,并加入一些配置。 在下面的例子中,我們定義了基類 Person,然后定義了 Person 的派生類 Student, public class Person {public string Name { set; get; }public override string ToString(){return "This is Person.";} }public class Student:Person {public string School { set; get; }public override string ToString(){return "This is Student.";} }class Program {static void Main(string[] args){// 創建容器Spring.Context.Support.StaticApplicationContext context= new Spring.Context.Support.StaticApplicationContext();// 注冊context.RegisterPrototype("Person", typeof(Student), null);// 注冊一個單例類型context.RegisterSingleton("Alice", typeof(Person), null);Person person = context.GetObject("Person") as Person;Console.WriteLine(person);} }
在開發中,我們通常通過 XML 配置文件來完成配置。Spring.NET 提供了 Spring.Context.Support.XmlApplicationContext,此時,對象的配置信息寫在一個 xml 的配置文件中,當然了,這個配置文件有特定的格式,這些規定以 Xml Schema 的形式保存在 Spring.NET\doc\schema 文件夾的 spring-objects-1.3.xsd 中。 對于上面的例子,我們可以編寫如下的配置文件 objects.xml。 <?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="Person" type="Student"></object>
<object id="Alice" type="Person"></object>
</objects> 然后,在代碼中,就可以直接使用容器了。 Spring.Context.Support.XmlApplicationContext context= new Spring.Context.Support.XmlApplicationContext("objects.xml"); Person person = context.GetObject("Person") as Person;Console.WriteLine(person); 如果你覺得這還是比較麻煩的話,還可以在程序啟動的時候直接加載配置信息。
Spring.NET 提供了Spring.Context.Support.ContextHandler,幫助我們直接在啟動程序的時候加載配置信息。 實際的配置文件通過 spring 元素中 context 元素下的 resource 指定,文件的話使用 file:// 協議描述,還可以使用其它的協議。例如嵌入在程序集中的配置文件可以使用??assembly:// , 直接寫在配置文件中則為 config://。 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="file://objects.xml"/>
</context>
</spring>
</configuration> 在程序中就可以直接使用了。 Spring.Context.IApplicationContext context= Spring.Context.Support.ContextRegistry.GetContext();Person person = context.GetObject("Person") as Person;Console.WriteLine(person);
還可以不再使用另外的 Spring 配置文件,而是將所有的配置信息都保存在應用程序配置文件中。 這需要使用一個新的配置處理器 Spring.Context.Support.DefaultSectionHandler,它可以幫助我們解析 spring 配置信息。 此時的配置文件成為如下的形式,注意,現在的 resource 中使用 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"/>
</context>
<objects>
<object id="Person" type="Student"></object>
<object id="Alice" type="Person"></object>
</objects>
</spring>
</configuration> 主程序與第三種情況是一樣的。
甚至還可以混合使用外部配置文件和嵌入的配置信息。 <?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="file://objects.xml"/>
<resource uri="config://spring/objects"/>
</context>
<objects>
<object id="Alice" type="Person"></object>
</objects>
</spring>
</configuration> 下面是兩種常用容器的類圖。
一、編程方式的容器
在 Spring.NET 中,對于通過編程方式使用容器的環境,提供了 Spring.Context.Support.StaticApplicationContext,我們可以直接創建這個容器,并加入一些配置。 在下面的例子中,我們定義了基類 Person,然后定義了 Person 的派生類 Student, public class Person {public string Name { set; get; }public override string ToString(){return "This is Person.";} }public class Student:Person {public string School { set; get; }public override string ToString(){return "This is Student.";} }class Program {static void Main(string[] args){// 創建容器Spring.Context.Support.StaticApplicationContext context= new Spring.Context.Support.StaticApplicationContext();// 注冊context.RegisterPrototype("Person", typeof(Student), null);// 注冊一個單例類型context.RegisterSingleton("Alice", typeof(Person), null);Person person = context.GetObject("Person") as Person;Console.WriteLine(person);} }
二、Xml 方式容器
在開發中,我們通常通過 XML 配置文件來完成配置。Spring.NET 提供了 Spring.Context.Support.XmlApplicationContext,此時,對象的配置信息寫在一個 xml 的配置文件中,當然了,這個配置文件有特定的格式,這些規定以 Xml Schema 的形式保存在 Spring.NET\doc\schema 文件夾的 spring-objects-1.3.xsd 中。 對于上面的例子,我們可以編寫如下的配置文件 objects.xml。 <?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="Person" type="Student"></object>
<object id="Alice" type="Person"></object>
</objects> 然后,在代碼中,就可以直接使用容器了。 Spring.Context.Support.XmlApplicationContext context= new Spring.Context.Support.XmlApplicationContext("objects.xml"); Person person = context.GetObject("Person") as Person;Console.WriteLine(person); 如果你覺得這還是比較麻煩的話,還可以在程序啟動的時候直接加載配置信息。
三、通過應用程序配置文件來自動加載 Spring.NET 配置
Spring.NET 提供了Spring.Context.Support.ContextHandler,幫助我們直接在啟動程序的時候加載配置信息。 實際的配置文件通過 spring 元素中 context 元素下的 resource 指定,文件的話使用 file:// 協議描述,還可以使用其它的協議。例如嵌入在程序集中的配置文件可以使用??assembly:// , 直接寫在配置文件中則為 config://。 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
</sectionGroup>
</configSections>
<spring>
<context>
<resource uri="file://objects.xml"/>
</context>
</spring>
</configuration> 在程序中就可以直接使用了。 Spring.Context.IApplicationContext context= Spring.Context.Support.ContextRegistry.GetContext();Person person = context.GetObject("Person") as Person;Console.WriteLine(person);
四、將所有的配置信息都保存在應用程序配置文件中
還可以不再使用另外的 Spring 配置文件,而是將所有的配置信息都保存在應用程序配置文件中。 這需要使用一個新的配置處理器 Spring.Context.Support.DefaultSectionHandler,它可以幫助我們解析 spring 配置信息。 此時的配置文件成為如下的形式,注意,現在的 resource 中使用 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"/>
</context>
<objects>
<object id="Person" type="Student"></object>
<object id="Alice" type="Person"></object>
</objects>
</spring>
</configuration> 主程序與第三種情況是一樣的。
五、混合使用外部配置文件和嵌入的配置
甚至還可以混合使用外部配置文件和嵌入的配置信息。 <?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="file://objects.xml"/>
<resource uri="config://spring/objects"/>
</context>
<objects>
<object id="Alice" type="Person"></object>
</objects>
</spring>
</configuration> 下面是兩種常用容器的類圖。
總結
以上是生活随笔為你收集整理的配置 Spring.NET的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: API 2.0Switching Bas
- 下一篇: 每个程序员都应该经历一次软考