最近用到了 Spring.NET ,不過在第一次使用的時(shí)候就遇到了一些問題,打算整理成系列博客,不斷的總結(jié)和分享。
同時(shí)也非常感謝在學(xué)習(xí)過程中給予我非常多幫助的前輩們,具體太多可能不能一一列舉,不過如果是看到前輩們的幫助后解決的問題,會(huì)在博客中掛上相應(yīng)文章的鏈接。 Coding and Share!
Spring 框架本是 Java 平臺(tái)上一個(gè)應(yīng)用非常多的、開源的框架。雖然語言是固定的,但是好的方法應(yīng)該是通用的,于是 Spring 框架 就被程序員從 Java 平臺(tái)搬遷到了 .NET 平臺(tái)。
通過Spring.NET,我們可以用統(tǒng)一且透明的方式來配置應(yīng)用程序。Spring.NET 的重點(diǎn)是為中間層提供聲明式事務(wù)管理,以及一個(gè)功能齊全的 ASP.NET 擴(kuò)展框架。Spring.NET 是非侵入式的,代碼對(duì)框架本身不會(huì)產(chǎn)生任何依賴。
Spring.NET 能夠提供很多方面的功能,例如:控制反轉(zhuǎn)(英文縮寫為IoC)、依賴注入(DI)、面向方面編程(AOP)、數(shù)據(jù)訪問抽象, 以及 ASP.NET 集成等。
Spring.NET 核心:
Spring.Core 庫是框架的基礎(chǔ), 提供依賴注入功能。Spring NET中大多數(shù)類庫依賴或擴(kuò)展了Spring.Core的功能。IObjectFactory接口提供了一個(gè)簡(jiǎn)單而優(yōu)雅的工廠模式,移除了對(duì)單例和一些服務(wù)定位stub的必要。允許你將真正的程序邏輯與配置解耦。作為對(duì)IObjectFactory 的擴(kuò)展,IApplicationContext接口也在Spring.Core庫中。
? 本次開發(fā)環(huán)境: VS 2017 本次開發(fā)項(xiàng)目:.netframework控制臺(tái)項(xiàng)目
(1). 使用 Nuget 安裝 Spring.core 包
使用 Spring.NET 需要 Spring.Core 庫的支持,同時(shí)在 Nuget 中安裝 Spring.Core 包,會(huì)在項(xiàng)目上自動(dòng)引入相關(guān)的引用。
(2).創(chuàng)建相關(guān)文件。
本次創(chuàng)建兩個(gè)文件:IUserInfo 接口、UserInfo 實(shí)體類。使用 Spring 反射創(chuàng)建 UserInfo 類,使用 IUserInfo 接口調(diào)用反射創(chuàng)建的類。
//IUserInfo接口using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Demo{ public interface IUserInfo { string ShowMss(); }}
//UserInfo實(shí)體類 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Demo{ //實(shí)現(xiàn) IUserInfo接口 public class UserInfo : IUserInfo { public string ShowMss() { return "Hello Spring.NET"; } }}
重要的是在配置節(jié)里面的配置
<?xml version="1.0" encoding="utf-8" ?><configuration> <configSections> <!--sectionGroup節(jié)點(diǎn)一定要在緊跟著configuration下面第一個(gè)添加--> <sectionGroup name="spring"> <!--跟下面Spring.Net節(jié)點(diǎn)配置是一一對(duì)應(yīng)關(guān)系--> <section name="context" type="Spring.Context.Support.ContextHandler,Spring.Core"/> <section name="objects" type="Spring.Context.Support.DefaultSectionHandler,Spring.Core"/> </sectionGroup> </configSections> <!--Spring.Net節(jié)點(diǎn)配置--> <spring> <!--容器配置--> <context> <resource uri="config://spring/objects"/> </context> <objects> <!--objects里面放容器的所有節(jié)點(diǎn)--> <description>An example that demonstrates simple Ioc features.</description><!--描述--> <!--name 必須要唯一的,type = 類的全名稱,所在的程序集--> <object name="UserInfo" type="Demo.UserInfo,Demo"></object><!--咱們剛才創(chuàng)建的UserInfo實(shí)體類--> </objects> </spring> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" /> </startup></configuration>
獲取并執(zhí)行:
//Program 程序入口類//引用 : Spring.Context 和 Spring.Context.Support 兩個(gè)命名空間using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Spring.Context;using Spring.Context.Support;namespace Demo{ class Program { static void Main(string[] args) { IApplicationContext ctx = ContextRegistry.GetContext(); IUserInfo userInfo = (IUserInfo)ctx.GetObject("UserInfo"); Console.WriteLine(userInfo.ShowMsg()); Console.Read(); } }}
輸出結(jié)果:
sectionGroup
節(jié)點(diǎn)一定要在緊跟著configuration下面第一個(gè)添加.objects
節(jié)點(diǎn)中的 <object></object>
節(jié)點(diǎn),name="value"
value 值一定要唯一,type ="value1 , value2"
中 value1 是需要反射創(chuàng)建出來的類的全名稱,value2 是該類所在的程序集。打完收工,后續(xù)再補(bǔ)充。
聯(lián)系客服