C#.NET編程—-Spring.NET & NHibernate整合(一)

來源:互聯網
上載者:User

第一個Spring.NET的程式

 建立項目

項目名稱為:SpringSample,NameSpace為“OKEC.Sample.Spring”。

 添加HelloTest類

HelloTest.cs

using System;
namespace OKEC.Sample.Spring
{
    /// <summary>
    /// HelloTest 的摘要說明。
    /// </summary>
    public class HelloTest
    {
        public HelloTest()
        {
            //
            // TODO: 在此處添加建構函式邏輯
            //
        }
        public void Test()
        {
            Console.WriteLine("This is Spring.NET Sample Test!");
            Console.WriteLine("Please press Enter close the windows!");
            Console.ReadLine();//讓程式停留,斷行符號關閉。
        }
    }
}

添加Spring.NET的設定檔

檔案名稱:Spring_bean.xml,屬性設定為:內嵌資源/ Embedded Resource<?xml version="1.0" encoding="utf-8"?>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://www.springframework.net 
         http://www.springframework.net/xsd/spring-objects.xsd">
  <object id="Hello" type="OKEC.Sample.Spring.HelloTest,SpringSample" />
</objects>

建立Spring.NET的容器初始化對像

SpringContext.cs

using System;
using Spring.Core;
using Spring.Aop;
using System;
using Spring.Core;
using Spring.Aop;
using Spring.Context;
using Spring.Context.Support;
namespace OKEC.Sample.Spring
{
    /// <summary>
    /// SpringFactory 的摘要說明。
    /// </summary>
    public class SpringContext
    {
        public SpringContext()
        {
            //
            // TODO: 在此處添加建構函式邏輯
            //
        }
        private static bool isInit = false;
        private static IApplicationContext context;
        public static void init()
        {
            string[] xmlFiles = new string[1];            
            xmlFiles[0] = "assembly://SpringSample/OKEC.Sample.Spring/Spring_bean.xml";
            context = new XmlApplicationContext(xmlFiles);
            isInit = true;
        }
        public static IApplicationContext Context
        {
            get{
                if(!isInit)
                {
                    init();
                }
                return context;
            }
        }
    }
}

添加啟動程式

StartMain.csusing System;
namespace OKEC.Sample.Spring
{
    /// <summary>
    /// StartMain 的摘要說明。
    /// </summary>
    public class StartMain
    {
        public StartMain()
        {
            //
            // TODO: 在此處添加建構函式邏輯
            //
        }
        [STAThread]
        static void Main() 
        {
            //Startup Spring Content
            SpringContext.init();

            //Test Spring IOC
            HelloTest test = (HelloTest)SpringContext.Context.GetObject("Hello");
            test.Test();
        }
    }
}

運行程式

結果為:

This is Spring.NET Sample Test!
Please press Enter close the windows!

你的第一個Spring.NET的程式成功了!

文檔中的項目原始碼請從以下地址下載:
http://www.springframework.cn/read.php?fid=8&tid=2&toread=1
完整的文檔請下載PDF文檔:
http://www.springframework.cn/read.php?fid=2&tid=1&toread=1


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.