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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

配置Spring.Net框架開發(fā)環(huán)境_實(shí)用技巧

作者:.NET開發(fā)菜鳥 ? 更新時(shí)間: 2022-05-04 編程語(yǔ)言

一、下載DLL文件

去Spring的官方網(wǎng)站下載并解壓,然后直接添加dll文件的引用就可以了。在上一篇文章中,已經(jīng)介紹過(guò)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中,對(duì)于通過(guò)編程方式使用容器的環(huán)境,提供了Spring.Context.Support.StaticApplicationContext,我們可以直接創(chuàng)建這個(gè)容器,并加入一些配置。在下面的例子中,我們定義了一個(gè)接口 IAnimal,然后定義了兩個(gè)類Dog和Cat,分別實(shí)現(xiàn)IAnimal接口:

IAnimal接口:

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

namespace SpringDemo
{
    /// 
    /// 動(dòng)物接口
    /// 
    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類實(shí)現(xiàn)IAnimal接口
    /// 
    public class Dog:IAnimal
    {
        /// 
        /// 實(shí)現(xiàn)吃的方法
        /// 
        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類實(shí)現(xiàn)IAnimal接口
    /// 
    public class Cat:IAnimal
    {
        /// 
        /// 實(shí)現(xiàn)吃的方法
        /// 
        public void Eat()
        {
            Console.WriteLine("貓?jiān)诔燥?");
        }
    }
}

主程序中調(diào)用:

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)
        {
            // 創(chuàng)建容器
            Spring.Context.Support.StaticApplicationContext context = new Spring.Context.Support.StaticApplicationContext();
            // 注冊(cè)狗類
            context.RegisterPrototype("IAnimal", typeof(Dog), null);
            IAnimal animal = context.GetObject("IAnimal") as IAnimal;
            animal.Eat();
            Console.ReadKey();
        }
    }
}

結(jié)果:

如果想調(diào)用Cat類的Eat()方法,只需要修改注冊(cè)的代碼即可:

// 注冊(cè)Cat類
context.RegisterPrototype("IAnimal", typeof(Cat), null);

三、XML方式容器

在開發(fā)中,我們通常通過(guò)XML配置文件來(lái)完成配置。Spring.Net提供了Spring.Context.Support.XmlApplicationContext,此時(shí),對(duì)象的配置信息寫在一個(gè)XML的配置文件中,這個(gè)XML的配置文件有特定的格式,這些規(guī)定以XML Schema的形式保存在Spring.NET-1.3.1\Spring.NET\doc\schema文件夾的spring-objects-1.3.xsd中。

對(duì)于上面的例子,我們可以編寫如下的配置文件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)
        {
            // 直接使用容器:路徑使用的相對(duì)路徑
            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文件使用的是相對(duì)路徑,要修改XML的屬性,把復(fù)制到輸出目錄改成“始終復(fù)制”,否則加載XML文件的時(shí)候會(huì)提示找不到文件的錯(cuò)誤?;蛘呤褂肵ML文件的絕對(duì)路徑。

四、通過(guò)應(yīng)用程序配置文件來(lái)自動(dòng)加載Spring.Net配置

Spring.Net提供了Spring.Context.Support.ContextHandler幫助我們直接在啟動(dòng)程序的時(shí)候加載配置信息。實(shí)際的配置文件通過(guò)spring節(jié)點(diǎn)中context節(jié)點(diǎn)下的resource節(jié)點(diǎn)的uri屬性的值指定,文件的話使用file://協(xié)議描述,還可以使用其他的協(xié)議。例如嵌入在程序集中的配置文件可以使用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();
        }
    }
}

如果想使用其他實(shí)現(xiàn)類,直接修改ObjectSchema.xml文件即可。

五、將所有的配置信息都保存在應(yīng)用程序配置文件中

還可以不再使用另外的Spring配置文件(即ObjectSchema.xml),而是將所有的配置信息都保存在應(yīng)用程序配置文件中。
這需要使用一個(gè)新的配置處理器Spring.Context.Support.DefaultSectionHandler,它可以幫助我們解析spring配置信息。
此時(shí)的配置文件改成如下的形式,注意:現(xiàn)在的resource中使用config://表示使用配置文件中的信息。

在基于XML的工廠中,這些對(duì)象定義表現(xiàn)為一個(gè)或多個(gè)子節(jié)點(diǎn),它們的父節(jié)點(diǎn)必須是(按:objects節(jié)點(diǎn)的xmlns元素是必需的,必須根據(jù)不同的應(yīng)用添加不同的命名空間,以便有IDE的智能提示。



  
    
      
      

主程序中調(diào)用:

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