标签:提示 code spring 主程 http class prot 分享 通过
一、下载DLL文件
去Spring的官方网站下载并解压,然后直接添加dll文件的引用就可以了。在上一篇文章中,已经介绍过Spring.Net框架中需要使用到的dll文件。这些程序集文件位于Spring.NET-1.3.1\Spring.NET\bin\net\4.0\debug或Spring.NET-1.3.1\Spring.NET\bin\net\4.0\release中。
二、编程方式的容器
在Spring.Net中,对于通过编程方式使用容器的环境,提供了Spring.Context.Support.StaticApplicationContext,我们可以直接创建这个容器,并加入一些配置。在下面的例子中,我们定义了一个接口 IAnimal,然后定义了两个类Dog和Cat,分别实现IAnimal接口:
IAnimal接口:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SpringDemo 8 { 9 /// <summary> 10 /// 动物接口 11 /// </summary> 12 public interface IAnimal 13 { 14 /// <summary> 15 /// 吃的方法 16 /// </summary> 17 void Eat(); 18 } 19 }
Dog类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SpringDemo 8 { 9 /// <summary> 10 /// 定义Dog类实现IAnimal接口 11 /// </summary> 12 public class Dog:IAnimal 13 { 14 /// <summary> 15 /// 实现吃的方法 16 /// </summary> 17 public void Eat() 18 { 19 Console.WriteLine("狗在吃饭"); 20 } 21 } 22 }
Cat类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SpringDemo 8 { 9 /// <summary> 10 /// 定义Cat类实现IAnimal接口 11 /// </summary> 12 public class Cat:IAnimal 13 { 14 /// <summary> 15 /// 实现吃的方法 16 /// </summary> 17 public void Eat() 18 { 19 Console.WriteLine("猫在吃饭!"); 20 } 21 } 22 }
主程序中调用:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SpringDemo 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 // 创建容器 14 Spring.Context.Support.StaticApplicationContext context = new Spring.Context.Support.StaticApplicationContext(); 15 // 注册狗类 16 context.RegisterPrototype("IAnimal", typeof(Dog), null); 17 IAnimal animal = context.GetObject("IAnimal") as IAnimal; 18 animal.Eat(); 19 Console.ReadKey(); 20 } 21 } 22 }
结果:
如果想调用Cat类的Eat()方法,只需要修改注册的代码即可:
// 注册Cat类 context.RegisterPrototype("IAnimal", typeof(Cat), null);
三、XML方式容器
在开发中,我们通常通过XML配置文件来完成配置。Spring.Net提供了Spring.Context.Support.XmlApplicationContext,此时,对象的配置信息写在一个XML的配置文件中,这个XML的配置文件有特定的格式,这些规定以XML Schema的形式保存在Spring.NET-1.3.1\Spring.NET\doc\schema文件夹的spring-objects-1.3.xsd中。
对于上面的例子,我们可以编写如下的配置文件ObjectSchema.xml:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <objects xmlns="http://www.springframework.net"> 3 <object id="bll" type="SpringDemo.Dog,SpringDemo"></object> 4 </objects>
然后在代码中就可以直接使用容器了:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SpringDemo 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 // 直接使用容器:路径使用的相对路径 14 Spring.Context.Support.XmlApplicationContext context = new Spring.Context.Support.XmlApplicationContext("ObjectSchema.xml"); 15 IAnimal animal = context.GetObject("bll") as IAnimal; 16 animal.Eat(); 17 Console.ReadKey(); 18 } 19 } 20 }
如果想使用Cat类,直接修改ObjectSchema.xml文件就可以,把type修改为:type="SpringDemo.Cat,SpringDemo"。
注意:
上面的代码中加载XML文件使用的是相对路径,要修改XML的属性,把复制到输出目录改成“始终复制”,否则加载XML文件的时候会提示找不到文件的错误。或者使用XML文件的绝对路径。
四、通过应用程序配置文件来自动加载Spring.Net配置
Spring.Net提供了Spring.Context.Support.ContextHandler帮助我们直接在启动程序的时候加载配置信息。实际的配置文件通过spring节点中context节点下的resource节点的uri属性的值指定,文件的话使用file://协议描述,还可以使用其他的协议。例如嵌入在程序集中的配置文件可以使用assembly://,直接写在配置文件中则为config://。
配置文件:
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <sectionGroup name="spring"> 5 <!--定义上下文切面--> 6 <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/> 7 </sectionGroup> 8 </configSections> 9 <spring> 10 <context> 11 <!--使用ObjectSchema.xml文件里面的配置信息--> 12 <resource uri="file://ObjectSchema.xml"/> 13 </context> 14 </spring> 15 <startup> 16 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> 17 </startup> 18 </configuration>
程序中直接使用:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SpringDemo 8 { 9 class Program 10 { 11 12 static void Main(string[] args) 13 { 14 15 Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext(); 16 IAnimal animal = context.GetObject("bll") as IAnimal; 17 animal.Eat(); 18 Console.ReadKey(); 19 } 20 } 21 }
如果想使用其他实现类,直接修改ObjectSchema.xml文件即可。
五、将所有的配置信息都保存在应用程序配置文件中
还可以不再使用另外的Spring配置文件(即ObjectSchema.xml),而是将所有的配置信息都保存在应用程序配置文件中。
这需要使用一个新的配置处理器Spring.Context.Support.DefaultSectionHandler,它可以帮助我们解析spring配置信息。
此时的配置文件改成如下的形式,注意:现在的resource中使用config://表示使用配置文件中的信息。
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <configSections> 4 <sectionGroup name="spring"> 5 <!--定义上下文切面--> 6 <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/> 7 <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/> 8 </sectionGroup> 9 </configSections> 10 <spring> 11 <context> 12 <!--使用ObjectSchema.xml文件里面的配置信息--> 13 <resource uri="config://spring/objects"/> 14 </context> 15 <objects> 16 <object id="bll" type="SpringDemo.Cat,SpringDemo" /> 17 </objects> 18 </spring> 19 <startup> 20 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> 21 </startup> 22 </configuration>
主程序中调用:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SpringDemo 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 14 Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext(); 15 IAnimal animal = context.GetObject("bll") as IAnimal; 16 animal.Eat(); 17 Console.ReadKey(); 18 } 19 } 20 }
示例代码下载地址:http://files.cnblogs.com/files/dotnet261010/SpringDemo.rar
Spring.Net框架二:配置Spring.Net框架环境
标签:提示 code spring 主程 http class prot 分享 通过
原文地址:http://www.cnblogs.com/dotnet261010/p/7373788.html