1. 程式人生 > >Spring.net 控制反轉(IOC)依賴注入(DI)的使用 以及Config檔案的配置

Spring.net 控制反轉(IOC)依賴注入(DI)的使用 以及Config檔案的配置

                                                  IOC

一 、把   \Spring.Net\Spring.NET-2.0.0-M1\Spring.NET\bin\net\4.0\release下的 三個核心檔案   Spring.Core.dll   Spring.Core.xml  Spring.Core.pdb  以及錯誤日誌檔案Common.Logging.dll   放在解決方案根目錄下的lib資料夾,並在專案中新增兩個dll檔案的引用。  

     注意:1、configSections 節點必須是configuration節點中的第一個節點

          2、object節點必須在objects節點中

         3、如果在一個控制器中配置了多餘的 屬性  當訪問到這個控制器的時候會因為多餘的屬性在控制器中找不到對應的變數或屬性而報'ActionInfoService' node cannot be resolved for the specified context [Core.ProjectOA.WebApp.Controllers.HomeController].   這時候把提示中的控制器中的對應節點在xml config檔案中刪除即可(或者在程式中對應的控制器下新增對應的屬性) 所以 在寫程式碼過程中 xml檔案中控制器下節點要隨著用到隨著新增,一旦對應屬性被刪除了 要記得去config檔案中刪節點

      配置檔案app.config :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 
  <configSections> //   新增這個節點以及裡面的內容,固定不用改(<span style="font-size:24px;color:#ff0000;">注意這個節點必須是configuration中的第一個節點  否則會報錯</span>)
    <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>
   <startup> //這個節點本來是預設 的第一個節點   要讓位給configSections
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
  </startup>
  <spring>  //新增這個節點 
    <context>
      <resource uri="config://spring/objects"/>
    </context>
    <objects xmlns="http://www.springframework.net">
      <description>An  example that demonstrates simple IoC features.</description>
      <object name="節點名(不一定但最好是類名,<span style="color:#ff0000;">跟後面程式中GetObject(object節點名)方法的引數必須一致</span>)" type="名稱空間.要建立的類名,程式集">    </object>
    </objects>  //<span style="color:#ff0000;">注意object 在objects節點裡</span>
  </spring>
</configuration>

 程式中建立類的例項   

 IApplicationContext ctx = ContextRegistry.GetContext();//建立容器
 IUserInfoService userInfoService = (IUserInfoService)ctx.GetObject(object節點名); //建立例項

                                          DI

步驟:

1先匯入dll檔案 (主要是Spring開頭的,其他的可能會缺少,少哪個引哪個)   如圖

2 拷貝案例中config資料夾到專案中

3 修改config資料夾中controllers.xml相關配置資訊

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">

  <object type="Core.ProjectOA.WebApp.Controllers.UserInfoController, Core.ProjectOA.WebApp" singleton="false" >
    <property name="UserInfoService" ref="UserInfoService" />
  </object>
  <!--intentionally do NOT register the AccountController or the ValuesController with the container; demonstrates that the underlying
  default controller factory will properly (attempt to!) resolve all controllers not registered with Spring.NET
  using its default controller resolution behavoir-->
  <!--<object type="Spring.Mvc4QuickStart.Controllers.AccountController, Spring.Mvc4QuickStart" singleton="false" />-->
  <!--<object type="Spring.Mvc4QuickStart.Controllers.ValuesController, Spring.Mvc4QuickStart" singleton="false" />-->
</objects>
4  可以將config/controllers.xml檔案中配置分離(控制器的配置和業務類的配置分離)注意web.config檔案中也要修改新增相應的引用
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object name="UserInfoService" type="Core.ProjectOA.BLL.UserInfoService, Core.ProjectOA.BLL" singleton="false" >
  </object>
</objects>
5 修改web.config資料夾中配置資訊
<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4"/>
    </sectionGroup>
  </configSections>
  <spring>
    <context>
      <resource uri="file://~/Config/controllers.xml"/>
      <resource uri="file://~/Config/services.xml"/>
    </context>
  </spring>

6  修改Global檔案 繼承SpringMvcApplication
public class MvcApplication : SpringMvcApplication