配置Spring.Net框架开发环境

这篇文章介绍了配置Spring.Net框架开发环境的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、下载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接口:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringDemo { ///  /// 动物接口 ///  public interface IAnimal { ///  /// 吃的方法 ///  void Eat(); } }

Dog类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringDemo { ///  /// 定义Dog类实现IAnimal接口 ///  public class Dog:IAnimal { ///  /// 实现吃的方法 ///  public void Eat() { Console.WriteLine("狗在吃饭"); } } }

Cat类:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringDemo { ///  /// 定义Cat类实现IAnimal接口 ///  public class Cat:IAnimal { ///  /// 实现吃的方法 ///  public void Eat() { Console.WriteLine("猫在吃饭!"); } } }

主程序中调用:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringDemo { class Program { static void Main(string[] args) { // 创建容器 Spring.Context.Support.StaticApplicationContext context = new Spring.Context.Support.StaticApplicationContext(); // 注册狗类 context.RegisterPrototype("IAnimal", typeof(Dog), null); IAnimal animal = context.GetObject("IAnimal") as IAnimal; animal.Eat(); Console.ReadKey(); } } }

结果:

如果想调用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:

  

然后在代码中就可以直接使用容器了:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringDemo { class Program { static void Main(string[] args) { // 直接使用容器:路径使用的相对路径 Spring.Context.Support.XmlApplicationContext context = new Spring.Context.Support.XmlApplicationContext("ObjectSchema.xml"); IAnimal animal = context.GetObject("bll") as IAnimal; animal.Eat(); Console.ReadKey(); } } }

如果想使用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://。

配置文件:

    

程序中直接使用:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringDemo { class Program { static void Main(string[] args) { Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext(); IAnimal animal = context.GetObject("bll") as IAnimal; animal.Eat(); Console.ReadKey(); } } }

如果想使用其他实现类,直接修改ObjectSchema.xml文件即可。

五、将所有的配置信息都保存在应用程序配置文件中

还可以不再使用另外的Spring配置文件(即ObjectSchema.xml),而是将所有的配置信息都保存在应用程序配置文件中。
这需要使用一个新的配置处理器Spring.Context.Support.DefaultSectionHandler,它可以帮助我们解析spring配置信息。
此时的配置文件改成如下的形式,注意:现在的resource中使用config://表示使用配置文件中的信息。

在基于XML的工厂中,这些对象定义表现为一个或多个子节点,它们的父节点必须是(按:objects节点的xmlns元素是必需的,必须根据不同的应用添加不同的命名空间,以便有IDE的智能提示。

    

主程序中调用:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SpringDemo { class Program { static void Main(string[] args) { Spring.Context.IApplicationContext context = Spring.Context.Support.ContextRegistry.GetContext(); IAnimal animal = context.GetObject("bll") as IAnimal; animal.Eat(); Console.ReadKey(); } } }

示例代码下载

到此这篇关于配置Spring.Net框架开发环境的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持0133技术站。

以上就是配置Spring.Net框架开发环境的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » ASP编程