Spring.NET教程(七)——依赖对象的注入(应用篇)

一、属性注入

之前简单提到依赖注入的用途。在教程6中,发现object节点下使用了<propertyname="Tool"ref="computer"/>,而property标签正是用来属性注入的,而ref是用来标识关联到哪个object。而name属性是指属性名。如下:

  <object id="modernPerson" type="SpringNetIoCDi.ModernPerson, SpringNetIoCDI">
    <property name="Tool" ref="computer"/>
  </object>

值类型的注入是需要使用property节点的value属性。如下

    <property name="Friend">
      <object type="SpringNetDI.Person, SpringNetDI" >
    </object>

二、构造函数注入

构造器注入使用constructor-arg标签作为标识。同样具有属性注入相同的方式,使用name、ref、value作为构造器注入的属性。

如下:

    <constructor-arg name="argPerson" ref="person"/>
    <constructor-arg name="intProp" value="1"/>

代码:

    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));

            Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name));
            Console.WriteLine(string.Format("argPerson Age:{0}", argPerson.Age));

            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));
        }
    }

 XML

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object id="person" type="SpringNetDI.Person, SpringNetDI" >
    <!--属性值类型注入-->
    <property name="Name" value="Liu Dong" />
    <property name="Age" value="27" />
    <!--内联对象注入-->
    <property name="Friend">
      <object type="SpringNetDI.Person, SpringNetDI" >
        <property name="Name" value="Begger" />
        <property name="Age" value="27" />
        <property name="Friend" ref="person" />
      </object>
    </property>
  </object>
  <object id="personDao" type="SpringNetDI.PersonDao, SpringNetDI" >
    <!--构造器注入-->
    <constructor-arg name="argPerson" ref="person"/>
    <constructor-arg name="intProp" value="1"/>
  </object>
</objects>

App.config

<?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"/>
      <resource uri="~/../../Configs.xml"/>
    </context>
    <objects xmlns="http://www.springframework.net" />
  </spring>
  <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
</configuration>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值