Spring.NET 基本使用

来源:互联网 发布:网络大电影发行协议 编辑:程序博客网 时间:2024/06/04 19:51

以前一直在使用Spring,虽然知道有Spring.NET这个东西,一直也没有时间去尝试,最近想写一个"热拔插"的高扩展性的组件,想探索下使用Spring.NET,写了一个简单的Demo作为开始。

1.安装

使用NuGet命令行

Install-Package Spring.Core
2.Test

using System;using System.IO;using Spring.Context.Support;namespace SpringNetBasic{    public class GameChannel    {        public string ChannelName { set; get; }        public string ChannelId { set; get; }    }    public class ChannelAccount    {        public string AccountName { set; get; }        public string AccountId { set; get; }    }    public class TestDemo    {        public GameChannel Channel { set; get; }        public ChannelAccount Account { set; get; }        public string GetInfo()        {            return Channel.ChannelName + Account.AccountName;        }    }    class Program    {        static void Main(string[] args)        {            var context                = new XmlApplicationContext(Directory.GetCurrentDirectory() + @"\object.xml");            var test = context.GetObject("TestDemo") as TestDemo;            if (test != null) Console.WriteLine(test.GetInfo());                        Console.ReadKey();        }    }}
3.object.xml

<?xml version="1.0" encoding="utf-8" ?><objects xmlns="http://www.springframework.net">  <object id="GameChannel" type="SpringNetBasic.GameChannel">    <property name="ChannelName" value="aaa"/>  </object>  <object id="ChannelAccount" type="SpringNetBasic.ChannelAccount">    <property name="AccountName" value="bbb"/>  </object>  <object id="TestDemo" type="SpringNetBasic.TestDemo">    <property name="Channel" ref="GameChannel"/>    <property name="Account" ref="ChannelAccount"/>  </object></objects>

4.结果


5.总结

Spring.NET通过配置的形式完成以来注入,相比其他以来注入组件,不需要写Attribute,这里比较简洁,跟Spring几乎一模一样。


0 0