一、属性注入
上篇我们简单提到依赖注入的用途。回顾一下所讲内容,发现在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"/>
复制代码
程序的代码如下:
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public Person Friend { get; set; }
- }
复制代码
PersonDao
- 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));
- }
- }
复制代码
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" />
- </context>
-
- <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="Beggar"/>
- <property name="Age" value="23"/>
- <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>
-
- </spring>
-
- </configuration>
复制代码
Program
- class Program
- {
- static void Main(string[] args)
- {
- IApplicationContext ctx = ContextRegistry.GetContext();
-
- PersonDao dao = ctx.GetObject("personDao") as PersonDao;
- dao.Get();
-
- Console.ReadLine();
- }
- }
复制代码
输出效果如下:
分享到:
相关推荐
刘冬编写Spring.NET学习笔记25——整合Quartz.NET例子。 原文: http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_QuartzNet.html
Spring.NET学习笔记22——整合WCF(应用篇) http://www.cnblogs.com/GoodHelper/archive/2010/05/15/SpringNet_Wcf.html
刘冬编写Spring.NET学习笔记20——整合Remoting(应用篇)的例子 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/19/SpringNet_Remoting.html
刘冬编写的Spring.NET学习笔记21——整合WebService的例子。 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/19/SpringNet_WebService.html
刘冬编写的Spring.NET学习笔记26——整合NVelocity例子。 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/20/SpringNet_NVelocity.html
刘冬编写的Spring.NET整合NHibernate例子 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/18/SpringNet_NHibernate.html
Spring.NET事务配置模板。 原文出处:http://www.cnblogs.com/GoodHelper/archive/2009/11/16/SpringNet_Transaction.html
刘冬编写的Spring.NET,NHibernate,ASP.NET MVC例子 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/19/SpringNet_Nhibernate_AspNetMvc.html
Spring.NET事务管理机制 原文:http://www.cnblogs.com/GoodHelper/archive/2009/11/16/SpringNet_Transaction.html
Spring.NET实用技巧3——NHibernate分布式事务(上) 博客出处: http://www.cnblogs.com/GoodHelper/archive/2010/07/29/SpringNetDistributedTransaction1.html
Spring.NET学习笔记 - 刘冬.NET - 博客园
Spring.NET实用技巧4——NHibernate分布式事务(下) 博客出处:http://www.cnblogs.com/GoodHelper/archive/2010/07/30/SpringNetDistributedTransaction2.html
Spring.NET实用技巧1——基于Prevalence下的NHibernate二级缓存使用技巧 文章出处 http://www.cnblogs.com/GoodHelper/archive/2010/05/19/SpringNet_NHbernate_Prevalence.html
Spring.NET实用技巧5——WCF环境下的NHibernate分布式事务 。 博客园出处:http://www.cnblogs.com/GoodHelper/archive/2010/08/04/SpringNetWcfDistributedTransaction.html
Spring.NET学习笔记:资源有源代码程序及文档说明,基础篇、应用篇、扩展集成、企业架构体系实战等;基础篇包括:控制反转,环境搭建,实现简易IOC框架,对象的创建,容器中对象作用域,依赖注入,依赖对象的注入,...
spring.net学习笔记,一步一步教你学习spring.net
博客出处: http://www.cnblogs.com/GoodHelper/archive/2010/05/25/SpringNet_NHibernate_Config.html
Spring.NET为建立企业级应用提供了一套轻量级的解决方案。通过Spring.NET,我们可以用统一且透明的方式来配置应用程序。Spring.NET的重点是为中间层提供声明...Spring.NET是非侵入式的,代码对框架本身不会产生任何依赖
在我们使用Spring.NET框架的时候,首先需要实例化Spring.NET容器, 然后调用IoC容器IObjectFactory接口中GetObject方法获取容器中的对象。通过这一点就可以告诉我们制作IoC容器需要写一个获取XML文件内容的方法和申明...
Spring.NET以Java版的Spring框架为基础,将Spring.Java的核心概念与思想移植到了.NET平台上。 第一章 序言 第二章 简介 2.1.概述 2.2.背景 2.3.模块 2.4.许可证信息 2.5.支持 第三章 背景 3.1.控制反转 第...