C#简单应用spring的例子

接口定义


namespace SpringDemo
{
    interface IOper
    {
        void Say();
    }
}

此接口的两个实现
实现1

using System;

namespace SpringDemo
{
    class Impl1 : IOper
    {
        #region IOper 成员

        public void Say()
        {
            Console.WriteLine("I'm impl 1");
        }

        #endregion
    }
}

实现2

using System;

namespace SpringDemo
{
    class Impl1 : IOper
    {
        #region IOper 成员

        public void Say()
        {
            Console.WriteLine("I'm impl 1");
        }

        #endregion
    }
}

调用部分


namespace SpringDemo
{
    class App
    {
        public IOper oper { get; set; }
        public void Run()
        {
            this.oper.Say();
        }
    }
}

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns='http://www.springframework.net'
         xmlns:db="http://www.springframework.net/database"
         xmlns:tx="http://www.springframework.net/tx"
         default-autowire="byName" default-lazy-init="true">
	<object name="oper" type="SpringDemo.Impl2, SpringDemo"/>  
</objects>

主函数

using System;
using Spring.Context;
using Spring.Context.Support;

namespace SpringDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string config = "applicationContext.xml";
            IApplicationContext context = new XmlApplicationContext(config);
            IOper oper = (IOper)context.GetObject("oper");
            App a = new App();
            a.oper = oper;
            a.Run();
            Console.Read();
        }
    }
}

posted @ 2015-05-25 17:33  wardensky  阅读(1857)  评论(0编辑  收藏  举报