網站首頁 編程語言 正文
一、下載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的工廠中,這些對象定義表現為一個或多個
主程序中調用:
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
相關推薦
- 2022-04-19 asp.core?同時兼容JWT身份驗證和Cookies?身份驗證兩種模式(示例詳解)_實用技巧
- 2022-06-22 C++深入探究類與對象之友元與運算符重載_C 語言
- 2022-05-21 Python遞歸時間復雜度_python
- 2022-11-18 python標準庫?datetime的astimezone設置時區遇到的坑及解決_python
- 2023-10-17 關于for循環遍歷不同表單校驗的bug(需要多次校驗)
- 2022-05-06 C語言中#define定義的標識符和宏實例代碼_C 語言
- 2022-04-07 C++中類的默認成員函數詳解_C 語言
- 2023-01-06 使用find命令快速定位配置文件位置_linux shell
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支