Spring.NET 依赖对象的注入

/*
 * 由SharpDevelop创建。
 * 用户: Changweihua
 * 日期: 2013/6/9
 * 时间: 11:33
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
using Spring.Context;
using Spring.Context.Support;
/// <summary>
/// 依赖对象的注入
/// </summary>
namespace SpringNET7
{
    class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                IApplicationContext ctx = ContextRegistry.GetContext();
                PersonDao dao = ctx.GetObject("personDao") as PersonDao;
                dao.Get();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("------------------------------------");
                Console.WriteLine(ex.StackTrace);
            }
                               
                               
                               
            Console.ReadKey(true);
        }
    }
}

<?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>
    </configSections>
               
    <spring>
        <context>
            <resource uri="config://spring/objects" />
        </context>
               
        <objects xlmns="http://www.springframework.net">
            <object id="friend" type="SpringNET7.Person, SpringNET7">
                <property name="Name" value="Chang VH" />
                <property name="Age" value="23" />
                <property name="Friend" ref="person" />
            </object>
            <object id="person" type="SpringNET7.Person, SpringNET7">
                <!-- Property Value Injection -->
                <property name="Name" value="Chang Weihua" />
                <property name="Age" value="23" />
                       
                <property name="Friend" ref="friend" />
                           
                <!-- Nested Object Injection -->
                <!--<property name="Friend">
                    <object type="SpringNET7.Person, SpringNET7" />
                        <property name="Name" value="Chang VH" />
                        <property name="Age" value="23" />
                        <property name="Friend" ref="person" />
                    </object>
                </property>-->
            </object>
                       
            <object id="personDao" type="SpringNET7.PersonDao, SpringNET7">
                <!-- Construction Injection -->
                <constructor-arg name="argPerson" ref="person" />
                <constructor-arg name="intProp" value="1" />
            </object>
        </objects>
    </spring>
               
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
    </startup>
</configuration>

/*
 * 由SharpDevelop创建。
 * 用户: Changweihua
 * 日期: 2013/6/9
 * 时间: 11:37
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
namespace SpringNET7
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Person Friend { get; set; }
    }
       
    public class PersonDao
    {
        private Person argPerson;
           
        private int intProp;
           
        public PersonDao(Person argPerson, int intProp)
        {
            this.argPerson = argPerson;
            this.intProp = intProp;
        }
           
        public void Get()
        {
            //构造函数注入的整型参数
            Console.WriteLine(string.Format("intProp:{0}", intProp));
            //构造函数注入的Person
            Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name));
            Console.WriteLine(string.Format("argPerson Age:{0}", argPerson.Age));
            //内联对象Friend
            Console.WriteLine(string.Format("argPerson Friend Name:{0}", argPerson.Friend.Name));
            Console.WriteLine(string.Format("argPerson Friend Age:{0}", argPerson.Friend.Age));
            //内联对象的循环引用
            Console.WriteLine(string.Format("argPerson Friend Friend Name:{0}", argPerson.Friend.Friend.Name));
            Console.WriteLine(string.Format("argPerson Friend Friend Age:{0}", argPerson.Friend.Friend.Age));
        }
           
    }
       
}


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

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

Tab Content 5

开发技术


开发平台和工具

sitemap     173.98ms