Spring.NET学习笔记7——依赖对象的注入(基础篇)

本文详细介绍了依赖注入中的属性注入和构造函数注入两种方式,并通过具体的代码示例展示了如何在配置文件中进行属性和构造函数的注入。

一、属性注入

  上篇我们简单提到依赖注入的用途。回顾一下所讲内容,发现在object节点下使用了<property name="Tool" ref="computer"/>。而property 标签正是用来属性注入的。而ref是用来标识是关联到哪个object。而name属性是指属性名。如下:

  1. <object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
  2.         <property name="Tool" ref="computer"/>
  3. </object>
复制代码

值类型的注入是需要使用property 节点的value属性。如<property name="Name" value="Liu Dong"/>

作为内联类型可以使用如下:

  1. <property name="Friend">
  2.           <object type="SpringNetDi.Person, SpringNetDi"/>
  3. </property>
复制代码

同理,内联类型可以是循环引用的对象(见代码处)。

二、构造函数注入

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

  1. <constructor-arg name="argPerson" ref="person"/>
  2. <constructor-arg name="intProp" value="1"/>
复制代码

程序的代码如下:

  1.     public class Person
  2.     {
  3.         public string Name { get; set; }
  4.         public int Age { get; set; }
  5.         public Person Friend { get; set; }
  6.     }
复制代码

PersonDao

  1.     public class PersonDao
  2.     {
  3.         private Person argPerson;
  4.         private int intProp;
  5.         public PersonDao(Person argPerson, int intProp)
  6.         {
  7.             this.argPerson = argPerson;
  8.             this.intProp = intProp;
  9.         }
  10.         public void Get()
  11.         {
  12.             //构造函数注入的整型参数
  13.             Console.WriteLine(string.Format("intProp:{0}", intProp));
  14.             //构造函数注入的Person
  15.             Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name));
  16.             Console.WriteLine(string.Format("argPerson Age:{0}", argPerson.Age));
  17.             //内联对象Friend
  18.             Console.WriteLine(string.Format("argPerson Friend Name:{0}", argPerson.Friend.Name));
  19.             Console.WriteLine(string.Format("argPerson Friend Age:{0}", argPerson.Friend.Age));
  20.             //内联对象的循环引用
  21.             Console.WriteLine(string.Format("argPerson Friend Friend Name:{0}", argPerson.Friend.Friend.Name));
  22.             Console.WriteLine(string.Format("argPerson Friend Friend Age:{0}", argPerson.Friend.Friend.Age));
  23.         }
  24.     }
复制代码

App.config

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3.   <configSections>
  4.     <sectionGroup name="spring">
  5.       <section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
  6.       <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
  7.     </sectionGroup>
  8.   </configSections>
  9.   <spring>
  10.     <context>
  11.       <resource uri="config://spring/objects" />
  12.     </context>
  13.     <objects xmlns="http://www.springframework.net">
  14.       <object id="person" type="SpringNetDi.Person, SpringNetDi">
  15.         <!--属性值类型注入-->
  16.         <property name="Name" value="Liu Dong"/>
  17.         <property name="Age" value="27"/>
  18.         <!--内联对象注入-->
  19.         <property name="Friend">
  20.           <object type="SpringNetDi.Person, SpringNetDi">
  21.             <property name="Name" value="Beggar"/>
  22.             <property name="Age" value="23"/>
  23.             <property name="Friend" ref="person"/>
  24.           </object>
  25.         </property>
  26.        
  27.       </object>
  28.       <object id="personDao" type="SpringNetDi.PersonDao, SpringNetDi">
  29.         <!--构造器注入-->
  30.         <constructor-arg name="argPerson" ref="person"/>
  31.         <constructor-arg name="intProp" value="1"/>
  32.        
  33.       </object>
  34.     </objects>
  35.   </spring>
  36. </configuration>
复制代码

Program

  1.     class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             IApplicationContext ctx = ContextRegistry.GetContext();
  6.             PersonDao dao = ctx.GetObject("personDao") as PersonDao;
  7.             dao.Get();
  8.             Console.ReadLine();
  9.         }
  10.     }
复制代码

输出效果如下:

转载于:https://www.cnblogs.com/hliq/archive/2011/03/26/2087220.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值