配置 Spring.NET 容器
作为一个容器,当然首先要存在一个容器对象了。Spring.NET 中的容器定义在程序集 Spring.Core 中,直接添加这个程序集的引用就可以开始使用了
引用方式:

一、编程方式的容器
第一步:定义 二个测试类
public class Person { public string Name { set; get; } public override string ToString() { return "This is Person."; } }
1 public class Student 2 { 3 public string Name { set; get; } 4 public override string ToString() 5 { 6 return "This is Student."; 7 } 8 }
第二步:引用容器程序集Spring.Core
第三步:调用
using Common; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 编程方式的容器01 { class Program { static void Main(string[] args) { // 创建容器 Spring.Context.Support.StaticApplicationContext context = new Spring.Context.Support.StaticApplicationContext(); // 注册 context.RegisterPrototype("Student", typeof(Student), null); // 注册一个单例类型 context.RegisterSingleton("Person", typeof(Person), null); Student Student = context.GetObject("Student") as Student; Person Person = context.GetObject("Person") as Person; Console.WriteLine(Person); Console.WriteLine(Student); } } }
二、Xml 方式容器
在开发中,我们通常通过 XML 配置文件来完成配置。Spring.NET 提供了 Spring.Context.Support.XmlApplicationContext,此时,对象的配置信息写在一个 xml 的配置文件中,当然了,这个配置文件有特定的格式,这些规定以 Xml Schema 的形式保存在 Spring.NET\doc\schema 文件夹的 spring-objects-1.3.xsd 中。
对于上面的例子,我们可以编写如下的配置文件 objects.xml。
配置XML注意二点:
第一步:XML存储路径,如果程序读取不了XML就会报错
第二点:必须使用正确的格式,否则报错
<object id="Student" type="Common.Student,Common"></object>
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net"> 3 4 <!--错误写法--> 5 <!--<object id="Student" type="Student"></object> 6 <object id="Person" type="Person"></object>--> 7 8 <!--<object id="名称" type="命名空间.类名,程序集名称"></object>--> 9 <object id="Student" type="Common.Student,Common"></object> 10 <object id="Person" type="Common.Person,Common"></object> 11 </objects>
然后,在代码中,就可以直接使用容器了。
1 using Common; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace Xml方式容器02 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 15 16 Spring.Context.Support.XmlApplicationContext context 17 = new Spring.Context.Support.XmlApplicationContext("objects.xml"); 18 Student Student = context.GetObject("Student") as Student; 19 Person Person = context.GetObject("Person") as Person; 20 Console.WriteLine(Person); 21 Console.WriteLine(Student); 22 Console.ReadLine(); 23 24 } 25 26 } 27 }
如果你觉得这还是比较麻烦的话,还可以在程序启动的时候直接加载配置信息。
三、通过应用程序配置文件来自动加载 Spring.NET 配置
Spring.NET 提供了Spring.Context.Support.ContextHandler,帮助我们直接在启动程序的时候加载配置信息。
实际的配置文件通过 spring 元素中 context 元素下的 resource 指定,文件的话使用 file:// 协议描述,还可以使用其它的协议。例如嵌入在程序集中的配置文件可以使用 assembly:// , 直接写在配置文件中则为 config://。
如果你 的程序是ASP.NET程序 <resource uri="~/App_Data/Config/Spring/Common.xml" />
<?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> <!--读取objects.xml配置文件实现对象注入容器--> <resource uri="file://objects.xml"/> </context> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
四、将所有的配置信息都保存在应用程序配置文件中
还可以不再使用另外的 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="Student" type="Common.Student,Common"></object> <object id="Person" type="Common.Person,Common"></object> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> </configuration>
五、混合使用外部配置文件和嵌入的配置
甚至还可以混合使用外部配置文件和嵌入的配置信息。
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 4 5 <configSections> 6 <sectionGroup name="spring"> 7 <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/> 8 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" /> 9 </sectionGroup> 10 </configSections> 11 12 <spring> 13 <context> 14 <resource uri="file://objects.xml"/> 15 <resource uri="config://spring/objects"/> 16 </context> 17 <objects> 18 <object id="Person" type="Common.Person,Common"></object> 19 </objects> 20 </spring> 21 22 <startup> 23 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 24 </startup> 25 </configuration>
学习出错截图


参考网址http://www.cnblogs.com/haogj/archive/2011/06/10/2077540.html
下载地址:http://pan.baidu.com/s/1c1BXgXU
浙公网安备 33010602011771号