Spring.NET 方法的注入

/*
 * 由SharpDevelop创建。
 * 用户: Changweihua
 * 日期: 2013/6/9
 * 时间: 23:11
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
using System.Reflection;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory.Support;
/// <summary>
/// 方法的注入
/// </summary>
namespace SpringNET10
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                IApplicationContext ctx = ContextRegistry.GetContext();
                PersonDao personDao = ctx.GetObject("personDao") as PersonDao;
                personDao.Save();
                           
                Console.WriteLine("-------------------------------------------------");
                           
                UserDao userDao = ctx.GetObject("userDao") as UserDao;
                Console.WriteLine(userDao.GetValue("测试替换任意方法"));
                           
                Console.WriteLine("-------------------------------------------------");
                           
                Door door = (Door)ctx.GetObject("door");
                door.OnOpen("Opening!");
                           
                           
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
                       
            Console.ReadKey(true);
        }
    }
               
    #region 查询方法注入
               
    public abstract class ObjectFactory
    {
        public abstract PersonDao CreatePersonDao();
    }
               
    public class PersonDao
    {
        public void Save()
        {
            Console.WriteLine("保存数据");
        }
    }
               
    #endregion
               
               
    #region 替换任意方法注入
               
    public class UserDao
    {
        public virtual string GetValue(string input)
        {
            return null;
        }
    }
               
    public class ReplaceValue : IMethodReplacer
    {
        public object Implement(object target, MethodInfo methodInfo, object[] arguments)
        {
            string value = (string)arguments[0];
            return "获取到" + value;
        }
    }
               
    #endregion
               
               
    #region 事件注入
               
    public delegate string OpenHandler(string arg);
               
    public class Door
    {
        public event OpenHandler OpenTheDoor;
                   
        public void OnOpen(string arg)
        {
            if(OpenTheDoor != null)
            {
                Console.WriteLine(OpenTheDoor(arg));
            }
        }
    }
               
    public class Men
    {
        public string OpenThisDoor(string arg)
        {
            return "参数是: " + arg;
        }
    }
               
    #endregion
               
}

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <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>
        <sectionGroup name="common">
            <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
        </sectionGroup>
    </configSections>
       
    <spring>
        <context>
            <resource uri="config://spring/objects" />
        </context>
       
        <objects xlmns="http://www.springframework.net">
            <!-- Query Method Injection -->
            <object id="personDao" type="SpringNET10.PersonDao, SpringNET10" singleton="false" />
            <object id="objectFactory" type="SpringNET10.ObjectFactory, SpringNET10">
                <lookup-method name="CreatePersonDao" object="personDao" />
            </object>
           
            <!-- Replace All Method Injection -->
            <object id="userDao" type="SpringNET10.UserDao, SpringNET10">
                <replaced-method name="GetValue" replacer="replaceValue">
                    <arg-type match="String" />
                </replaced-method>
            </object>
            <object id="replaceValue" type="SpringNET10.ReplaceValue, SpringNET10" />
           
            <object id="men" type="SpringNET10.Men, SpringNET10">
                <listener event="OpenTheDoor" method="OpenThisDoor">
                    <ref object="door" />
                </listener>
            </object>
            <object id="door" type="SpringNET10.Door, SpringNET10" />
           
        </objects>
    </spring>
    <common>
        <logging>
            <factoryAdapter type="Common.Logging.Simple.TraceLoggerFactoryAdapter, Common.Logging">
                <arg key="level" value="ALL" />
                <arg key="showLogName" value="true" />
                <arg key="showDataTime" value="true" />
                <arg key="dateTimeFormat" value="yyyy/MM/dd HH:mm:ss:fff" />
            </factoryAdapter>
        </logging>
    </common>
       
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    </startup>
</configuration>


知识共享许可协议
《Spring.NET 方法的注入》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     152.09ms