學習Spring.NET 例子

來源:互聯網
上載者:User
準備工作

現在手頭還沒有Spring.NET的朋友,先讓我們來到這裡下載一份(出於功能考慮推薦使用1.1 Preview 3版的)

如同使用其他第三方工具一樣,依據.NET版本引用好Spring.Core.dll程式集,然後在設定檔中加入這麼一段

step 1:<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">

</objects>
</spring>

OK,第一步工作做好.

不知道,各位在外漂泊的單身漢是不是有跟我一樣的苦惱: 快餐吃來吃去,晚上經常不知道吃啥好,得,我就拿這個做DEMO吧.

先定義一個介面咯

step2:public interface 晚上吃啥
{
   void 吃飯();
}

口味比較重,一般比較喜歡[吃辣椒炒肉褒仔飯] 和 [吃水煮魚]

step3:public class 吃辣椒炒肉煲仔飯 : 晚上吃啥
{
    public void 吃飯()
   { 
     Console.WriteLine("吃辣椒炒肉煲仔飯咯");
   }
}

public class 吃水煮魚 : 晚上吃啥
{
   public void 吃飯()
   { 
     Console.WriteLine("吃水煮魚咯");
   }
}

寫好了,我也決定了:水煮魚
在config檔案的<objects></objects>之間加上一句

step 4:<objects xmlns="http://www.springframework.net">

<object id="晚餐" type="SpringDemo_1.吃水煮魚,SpringDemo_1" />

</objects>

美食當前,具體怎麼享用呢? 看這裡

step 5:[STAThread]
        static void Main(string[] args)
        {
            Spring.Context.IApplicationContext ctx = 
                Spring.Context.Support.ContextRegistry.GetContext();

            晚上吃啥 temp = ctx["晚餐"] as 晚上吃啥;

            temp.吃飯();
        }

運行一下, 看到結果了吧    hoho~~

--------------------------------------------------------------------------
現在讓我們回過過頭來看看,具體每一步都做了什麼

step 1:
<configSections></configSections> 配置節,定義了<spring>配置組
<context><resource uri="{0}" /></context>  
Spring.NET裡預設提供了三種資源描述方式:
1)  <resource uri="config://spring/objects" />
     表示在config設定檔的<spring><objects>配置節內部定義了<object />
2) <resource uri="~/yourAppContext.xml" />
      表示在程式的根目錄下的yourAppContext.xml檔案裡定義了<object />
3)<resource uri="assembly://assemblyName/namespace/yourAppContext.xml" />
      表示yourAppContext.xml設為了嵌入式資源,並在其中定義了<object />

step 2:

自訂的行為規範

step 3:

行為規範的具體實現

step 4 :

通過Spring.NET的IoC容器來配置由哪個符合行為規範的對象來執行行為

step 5:

執行行為,具體由誰執行,調用者(main()方法)不知情。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.