Spring.Net学习笔记六(依赖对象的注入)
一、属性注入
上篇我们简单提到依赖注入的用途。回顾一下所讲内容,发现在object节点下使用了<property name="Tool" ref="computer"/>。而property 标签正是用来属性注入的。而ref是用来标识是关联到哪个object。而name属性是指属性名。如下:
<object id="modernPerson" type="SpringNetIoC.ModernPerson, SpringNetIoC">
<property name="Tool" ref="computer"/>
</object>
值类型的注入是需要使用property 节点的value属性。如<property name="Name" value="Liu Dong"/>
作为内联类型可以使用如下:
<property name="Friend">
<object type="SpringNetDi.Person, SpringNetDi"/>
</property>
同理,内联类型可以是循环引用的对象(见代码处)。
二、构造函数注入
构造器注入使用constructor-arg标签作为标识。同样具有于属性注入相同的方式,使用name、ref、value作为构造器注入的属性,如下:
<constructor-arg name="argPerson" ref="person"/>
<constructor-arg name="intProp" value="1"/>
类库代码:
1 namespace Dao.IOC 2 { 3 public class PersonDao 4 { 5 private Person argPerson; 6 private int intProp; 7 public PersonDao(Person argPerson, int intProp) 8 { 9 this.argPerson = argPerson; 10 this.intProp = intProp; 11 } 12 public void Get() 13 { 14 //构造函数注入的整型参数 15 Console.WriteLine(string.Format("intProp:{0}", intProp)); 16 //构造函数注入的Person 17 Console.WriteLine(string.Format("argPerson Name:{0}", argPerson.Name)); 18 Console.WriteLine(string.Format("argPerson Age:{0}", argPerson.Age)); 19 //内联对象Friend 20 Console.WriteLine(string.Format("argPerson Friend Name:{0}", argPerson.Friend.Name)); 21 Console.WriteLine(string.Format("argPerson Friend Age:{0}", argPerson.Friend.Age)); 22 //内联对象的循环引用 23 Console.WriteLine(string.Format("argPerson Friend Friend Name:{0}", argPerson.Friend.Friend.Name)); 24 Console.WriteLine(string.Format("argPerson Friend Friend Age:{0}", argPerson.Friend.Friend.Age)); 25 } 26 } 27 28 public class Person 29 { 30 public string Name { get; set; } 31 public int Age { get; set; } 32 public Person Friend { get; set; } 33 } 34 }
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 <!--容器配置--> 12 <resource uri="config://spring/objects"/> 13 </context> 14 <objects xmlns="http://www.springframework.net"> 15 <!--这里放容器里面的所有节点--> 16 <description>An example that demonstrates simple IoC features.</description> 17 <!--name 必须要唯一的,type=类的全名称,所在的程序集--> 18 <object name="person" type="Dao.IOC.Person,Dao"> 19 <property name="Name" value="Liu Dong"/> 20 <property name="Age" value="27"/> 21 <property name="Friend"> 22 <object type="Dao.IOC.Person,Dao"> 23 <property name="Name" value="Beggar"/> 24 <property name="Age" value="23"/> 25 <property name="Friend" ref="person"/> 26 </object> 27 </property> 28 </object> 29 <object name="personDao" type="Dao.IOC.PersonDao,Dao"> 30 <constructor-arg name="argPerson" ref="person"/> 31 <constructor-arg name="intProp" value="1"/> 32 </object> 33 </objects> 34 </spring> 35 <startup> 36 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" /> 37 </startup> 38 </configuration>
1 static void Main(string[] args) 2 { 3 4 //IApplicationContext ctx = ContextRegistry.GetContext(); 5 //Person person = (Person)ctx.GetObject("modernPerson"); 6 //person.Work(); 7 //Console.ReadLine(); 8 9 IApplicationContext ctx = ContextRegistry.GetContext(); 10 Dao.IOC.PersonDao dao = ctx.GetObject("personDao") as Dao.IOC.PersonDao; 11 dao.Get(); 12 Console.ReadLine(); 13 }
在这里尤其要注意配置文件的空格问题,最好建议是手敲而不是简单的拷贝。