IOC容器咱們只停留在知道上是不行的,咱們要動手作印象對更深入,那麼我給你們看一個代碼。看看代碼中IOC容器的實現。spring
建立一個類庫:設計模式
解決方式的類庫創建:post
建立一個實體類:User:學習
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Text; namespace Spring.Demo.Model { /// <summary> /// 用戶類 /// </summary> public class Users { /// <summary> /// 編號 /// </summary> private int _oid; public int Oid { get { return _oid; } set { _oid = value; } } /// <summary> /// 姓名 /// </summary> private string _name; public string Name { get { return _name; } set { _name = value; } } /// <summary> /// 性別 /// </summary> private string _sex; public string Sex { get { return _sex; } set { _sex = value; } } /// <summary> /// 年齡 /// </summary> private int _age; public int Age { get { return _age; } set { _age = value; } } } }</span>
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Text; namespace Spring.Demo.Service { public interface IUsers { /// <summary> /// 返回用戶的具體信息的方法 /// </summary> /// <returns></returns> string GetUserInfo(); } } </span>
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Text; using Spring.Demo.Service; using Spring.Demo.Model; namespace Spring.Demo.Compontext { public class UsersCompontents : IUsers { public UsersCompontents() { } #region 獲取用戶信息 public string GetUserInfo() { Users user = new Users(); user.Oid = 1; user.Name = "Beniao"; user.Sex = "Boy"; user.Age = 25; return string.Format("編號:{0}--->姓名:{1}--->性別:{2}--->年齡:{3}", user.Oid, user.Name, user.Sex, user.Age); } #endregion } }</span>
<span style="font-size:18px;">using ITOO.Library.Core.AOP; using Spring.Context; using Spring.Demo.Service; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; namespace Sping.Demo.SimpleTest { class Program { static void Main(string[] args) { IUsers studentChangeBll = SpringHelper.GetObject<IUsers>("Users"); Console.WriteLine(studentChangeBll.GetUserInfo()); Console.Read(); } } }</span>
<span style="font-size:18px;"><?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> <resource uri="config://spring/objects"/> </context> <objects xmlns="http://www.springframework.net"> <!--這的配置依據實際的程序來的。UsersCompontents是程序集Spring.Demo.Compontext下的一個類--> <object name="Users" type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontext" singleton="false" > </object> </objects> </spring> </configuration></span>spa
咱們通常寫代碼中咱們是這樣寫的: .net
<span style="font-size:18px;">//從config文件中取得程序集信息 IApplicationContext context = ConfigurationManager.GetSection("spring/context") as IApplicationContext; //調用方法 //Users爲config文件中的配置節 //<object name="Users" // type="Spring.Demo.Compontext.UsersCompontents,Spring.Demo.Compontent"> //</object> IUsers user = context.GetObject("Users") as IUsers;</span>
建立一個類:SpringHelper:設計
<span style="font-size:18px;">using System; using System.Collections.Generic; using System.Linq; using System.Text; using Spring.Context; using Spring.Context.Support; namespace ITOO.Library.Core.AOP { public class SpringHelper { /// <summary> /// Spring容器上下文 /// </summary> private static IApplicationContext SpringContext { get { return ContextRegistry.GetContext(); } } /// <summary> /// 獲取配置文件 配置的 對象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="objName"></param> /// <returns></returns> public static T GetObject<T>(string objName) where T : class { return (T)SpringContext.GetObject(objName); } } } </span>
這裏體現了封裝的重要性。先前我在作AOP的時候。個人師傅看到了相似這種代碼的時候,他就跟我討論過這個問題,我當時懵懵懂懂,沒有進行下一步的行動,現在想一想,問題出現在我根本沒有動手去作,或者知識沒有深刻到那個層次,認識這個知識的方面沒有那麼深。code
所有問題,都要動手去作才行。
orm
咱們從上面的實踐到分析以後,咱們發現事實上咱們看似是新的東西,事實上咱們已經學習過了,就像IOC容器同樣,咱們學習過了反射和配置文件。咱們發現事實上IOC容器不就是反射和配置文件來實現的嗎,反射和配置文件是咱們在大話設計模式中就已經學習到了的東西,這都不是新的東西。一個看似複雜的東西,都是有簡單的東西來組裝成的。咱們知道這個。就不會對新的東西有畏懼感了。xml