配置Spring.Net框架环境

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

配置Spring.Net框架环境

.NET开发菜鸟   2022-05-23 我要评论

一、下载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
{
    /// <summary>
    /// 动物接口
    /// </summary>
    public interface IAnimal
    {
        /// <summary>
        /// 吃的方法
        /// </summary>
        void Eat();
    }
}

Dog类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpringDemo
{
    /// <summary>
    /// 定义Dog类实现IAnimal接口
    /// </summary>
    public class Dog:IAnimal
    {
        /// <summary>
        /// 实现吃的方法
        /// </summary>
        public void Eat()
        {
            Console.WriteLine("狗在吃饭");
        }
    }
}

Cat类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpringDemo
{
    /// <summary>
    /// 定义Cat类实现IAnimal接口
    /// </summary>
    public class Cat:IAnimal
    {
        /// <summary>
        /// 实现吃的方法
        /// </summary>
        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:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
    <object id="bll" type="SpringDemo.Dog,SpringDemo"></object>
</objects>

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

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://。

配置文件:

<?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>
       <!--使用ObjectSchema.xml文件里面的配置信息-->
      <resource uri="file://ObjectSchema.xml"/>
    </context>
  </spring>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    </startup>
</configuration>

程序中直接使用:

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

<?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>
       <!--使用ObjectSchema.xml文件里面的配置信息-->
      <resource uri="config://spring/objects"/>
    </context>
    <objects>
      <object id="bll" type="SpringDemo.Cat,SpringDemo" />
    </objects>
  </spring>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
    </startup>
</configuration>

主程序中调用:

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框架开发环境的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们