日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

配置Spring.Net框架開發環境_實用技巧

作者:.NET開發菜鳥 ? 更新時間: 2022-05-04 編程語言

一、下載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();
        }
    }
}

示例代碼下載

原文鏈接:https://www.cnblogs.com/dotnet261010/p/7373788.html