Spring.NET 自定义对象行为

/*
 * 由SharpDevelop创建。
 * 用户: Changweihua
 * 日期: 2013/6/10
 * 时间: 10:27
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
using Spring.Context;
using Spring.Context.Support;
using Spring.Objects.Factory;
/// <summary>
/// 自定义对象行为
/// </summary>
namespace SpringNET11
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                using(IApplicationContext ctx = ContextRegistry.GetContext())
                {
                    Child child = ctx.GetObject("child") as Child;
                    Console.WriteLine("Child中Name的值为: {0}", child.Name);
                }
                           
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
            }
                       
            Console.ReadKey(true);
        }
                   
                   
    }
               
    #region 生命周期接口
               
    public class Person
    {
        public void Init()
        {
            Console.WriteLine("我长大了");
        }
        public void Destroy()
        {
            Console.WriteLine("我衰老了");
        }
    }
               
               
    public class Student : IDisposable, IInitializingObject
    {
        public void AfterPropertiesSet()
        {
            Console.WriteLine("设置完所有属性");
        }
                   
        public void Dispose()
        {
            Console.WriteLine("销毁对象");
        }
    }
               
    #endregion
               
    #region 抽象对象定义和子对象定义
               
    public class Parent
    {
        public string Name { get; set; }
    }
               
    public class Child
    {
        public string Name { get; set; }
    }
               
    #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">
           
            <!-- 生命周期接口 -->
            <object id="person" type="SpringNET11.Person, SpringNET11" init-method="Init" destroy-method="Destroy" />
            <object id="student" type="SpringNET11.Student, SpringNET11" />
           
            <!-- 抽象对象定义和子对象定义 -->
            <object id="parent" type="SpringNET11.Parent, SpringNET" abstract="true">
                <property name="Name" value="parent" />
            </object>
            <object id="child" type="SpringNET11.Child, SpringNET11" parent="parent" />
           
        </objects>
    </spring>
           
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    </startup>
</configuration>


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

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

Tab Content 5

开发技术


开发平台和工具

sitemap     156.57ms