`
feiliboos
  • 浏览: 662677 次
文章分类
社区版块
存档分类
最新评论

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. </object>
  27. <object id="personDao" type="SpringNetDi.PersonDao, SpringNetDi">
  28. <!--构造器注入-->
  29. <constructor-arg name="argPerson" ref="person"/>
  30. <constructor-arg name="intProp" value="1"/>
  31. </object>
  32. </objects>
  33. </spring>
  34. </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. }
复制代码

输出效果如下:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics