配置Spring.Net框架開發(fā)環(huán)境
一、下載DLL文件
去Spring的官方網(wǎng)站下載并解壓,然后直接添加dll文件的引用就可以了。在上一篇文章中,已經(jīng)介紹過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ì)于通過編程方式使用容器的環(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
{
/// <summary>
/// 動(dòng)物接口
/// </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類實(shí)現(xiàn)IAnimal接口
/// </summary>
public class Dog:IAnimal
{
/// <summary>
/// 實(shí)現(xiàn)吃的方法
/// </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類實(shí)現(xiàn)IAnimal接口
/// </summary>
public class Cat:IAnimal
{
/// <summary>
/// 實(shí)現(xiàn)吃的方法
/// </summary>
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ā)中,我們通常通過XML配置文件來完成配置。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:
<?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)
{
// 直接使用容器:路徑使用的相對(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ì)路徑。
四、通過應(yīng)用程序配置文件來自動(dòng)加載Spring.Net配置
Spring.Net提供了Spring.Context.Support.ContextHandler幫助我們直接在啟動(dòng)程序的時(shí)候加載配置信息。實(shí)際的配置文件通過spring節(jié)點(diǎn)中context節(jié)點(diǎn)下的resource節(jié)點(diǎn)的uri屬性的值指定,文件的話使用file://協(xié)議描述,還可以使用其他的協(xié)議。例如嵌入在程序集中的配置文件可以使用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();
}
}
}如果想使用其他實(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è)<object>子節(jié)點(diǎn),它們的父節(jié)點(diǎn)必須是<objects>(按:objects節(jié)點(diǎn)的xmlns元素是必需的,必須根據(jù)不同的應(yīng)用添加不同的命名空間,以便有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>主程序中調(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();
}
}
}到此這篇關(guān)于配置Spring.Net框架開發(fā)環(huán)境的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
asp.net GridView的刪除對(duì)話框的兩種方法
本來這兩種方法,我已經(jīng)掌握。但是沒有總結(jié),今天朋友突然問題,我竟然想不起來,找了半天,現(xiàn)在亡羊補(bǔ)牢,趕快寫在博客里。2009-04-04
為TextBox裝飾水印與(blur和focus)事件應(yīng)用
為了界面的美觀,有些時(shí)候可能需要為文本框(TextBox)裝飾個(gè)水?。凰袃煞N狀態(tài),一是blur和focus。因此,我們可以在Javascript寫兩個(gè)事件,感興趣的朋友可以了解下2013-01-01
ASP.NET百度Ueditor編輯器實(shí)現(xiàn)上傳圖片添加水印效果
這篇文章主要給大家介紹了ASP.NET百度Ueditor編輯器1.4.3這個(gè)版本實(shí)現(xiàn)上傳圖片添加水印效果的相關(guān)資料,文中通過圖文及示例代碼介紹的非常詳細(xì),相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03
詳解.Net中字符串不變性與相等判斷的特殊場(chǎng)景
本文主要介紹了.Net中字符串不變性與相等判斷的特殊場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
asp.net repeater實(shí)現(xiàn)批量刪除
asp.net repeater實(shí)現(xiàn)批量刪除實(shí)現(xiàn)效果代碼2009-03-03
asp.net 中國(guó)身份證號(hào)碼驗(yàn)證代碼 非正則
asp.net 中國(guó)身份證號(hào)碼驗(yàn)證,需要的朋友可以參考下。2009-11-11
JavaScript驗(yàn)證用戶輸入的是字符或數(shù)字及ASCII Chart應(yīng)用
我們可以根據(jù)onkeydown事件的event.keyCode即是ASCII Chart來判斷字符或數(shù)字等,本文提供了相關(guān)鍵盤key對(duì)應(yīng)的ASCII碼,以供用戶使用是參考及使用onpaste事件只能輸入字符和數(shù)字防止粘貼等的,感興趣的朋友可以了解下2013-01-01

