網(wǎng)站首頁(yè) 編程語(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è)
主程序中調(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
相關(guān)推薦
- 2022-06-30 python神經(jīng)網(wǎng)絡(luò)tensorflow利用訓(xùn)練好的模型進(jìn)行預(yù)測(cè)_python
- 2022-08-15 Dubbo3基礎(chǔ)配置安裝及整合Springboot
- 2023-04-07 C語(yǔ)言如何計(jì)算字符串長(zhǎng)度_C 語(yǔ)言
- 2022-12-26 Python常用標(biāo)準(zhǔn)庫(kù)之os模塊功能_python
- 2022-12-04 詳解Go?依賴管理?go?mod?tidy_Golang
- 2022-05-03 基于docker部署skywalking實(shí)現(xiàn)全鏈路監(jiān)控功能_docker
- 2024-02-29 UNI-APP中點(diǎn)擊事件多重響應(yīng)問(wèn)題的解決,list列表項(xiàng)item和列表項(xiàng)item中按鈕的點(diǎn)擊事件沖
- 2023-11-14 Docker 拉取結(jié)果為“等待連接時(shí)請(qǐng)求取消(等待標(biāo)頭時(shí)超出客戶端超時(shí));Error respons
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支