属性的注入:

  在上篇例子中已经出现并解释过:

 <object id="dog" type="SpringDemo.Dog,SpringDemo"  singleton="true">
<property name="name" value="旺财"></property>
</object>
或者

其中 <property name="name" value="旺财"></property>即使属性的注入

还有一种方式:
<object id="person" type="SpringDemo.Person,SpringDemo" singleton="true" >
<property name="pet" ref="dog" ></property> </object>

使用ref 标识是关联到哪个object

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

<property name="pet" >
<object type="SpringDemo.Dog,SpringDemo">
</object>
</property>
构造函数注入:

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

<constructor-arg name="pet" ref="person"/>
<constructor-arg name="age" value="1"/>

集合类型的注入:

  IDictionary类型

  使用<dictionary>元素来表示IDictionary接口的实现类型。<entry/>表示IDictionary集合的元素。key和value属性为元素的键值队,value-ref为关联的元素。

同理,<dictionary>元素的key-type和value-type属性来表示泛型IDictionary,例如 <dictionary key-type="string" value-type="object"> 。

  ILIst类型

  使用<list>元素作为ILIst的标签,value为集合中元素的值。也可以注入对象,甚至关联其它对象,使用 <ref/>元素表示关联的对象,object 属性为所关联对象的id或name。集合可以为空,用<null/>元素来标记。

  在<list>元素中设置 element-type 属性表示泛型T的类型,例如 element-type="int"  ,代表int型。

方法的注入:

  查询方法的注入:

  

 public class PersonDao
{
public void SaveMoney()
{
Console.WriteLine("存了点money");
}
}
 public abstract class ObjectFactory
{
public abstract PersonDao CreatePersonDao();
}
<object id="persondao" type="SpringDemo.PersonDao,SpringDemo"></object>
<object id="objectfactory" type="SpringDemo.ObjectFactory,SpringDemo">
<lookup-method name="CreatePersonDao" object="persondao"/>
</object>

调用:

   ObjectFactory factory = ctx.GetObject("objectfactory") as ObjectFactory;
factory.CreatePersonDao().SaveMoney();
Console.WriteLine();

执行结果:

Lookup Method Injection:看了上面的例子再解释会明朗一些。

  查询方法XML配置的lookup-method name中配置的方法名,一定会返回object中配置的对象。

  Spring.Net可以对动态的对目标对象的抽象方法或者虚方法进行覆盖,并且可以在容器类查找已命名的对象,查询方法注入就利用了这一功能。被查询的对象一般应该是非Singleton的,但是也可以是Singleton的。Spring.NET使用System.Reflecttion.Emit命名空间中的类型在运行时动态生成某个类的子类并覆盖其方法,以实现查询方法注入。被注入的方法应该是抽象无参数的或者虚方法,并且方法的可见性应该在Private以上(因为抽象方法或者虚方法设置为private就没有意义)。

  替换任意方法:

  

public class ManyRun : IMethodReplacer
{ public object Implement(object target, MethodInfo method, object[] arguments)
{ string value = (string)arguments[];
return "获取到:" + value;
}
}

继承 IMethodReplacer接口和 Implement方法 并实现方法

public class Dog : Pet
{
public string name { get; set; }
public void bark()
{
Console.WriteLine("汪汪汪汪汪汪汪汪汪...");
}
public virtual string run(string str)
{
Console.WriteLine(name+ "蹦跶蹦跶的跑..."+str);
return name + "蹦跶蹦跶的跑..." + str;
}
}

dog有个run方法:

  代码如下:

<object id="dog" type="SpringDemo.Dog,SpringDemo"  singleton="true">
<property name="name" value="旺财"></property>
<replaced-method name="run" replacer="manyrun" >
<arg-type match="String"/>
</replaced-method> </object>
<object id="manyrun" type="SpringDemo.ManyRun,SpringDemo"></object>

调用:

  

 Dog dog = ctx.GetObject("dog") as Dog;
Console.WriteLine(dog.run("凌波微步。。。"));

事件注入:

   在listener节点处配置event和method属性指明事件名和绑定的方法,并增加ref节点设置object属性来指明调用哪个IoC容器对象。

  代码:

//先定义一个委托
public delegate object frisbeehandler(object obj); public class Dog : Pet
{
public string name { get; set; }
public void bark()
{
Console.WriteLine("汪汪汪汪汪汪汪汪汪...");
}
public virtual string run(string str)
{
Console.WriteLine(name+ "蹦跶蹦跶的跑..."+str);
return name + "蹦跶蹦跶的跑..." + str;
}
public event frisbeehandler frisbeefly;
public void chasefrisbee(object obj)
{
//调用事件
if (frisbeefly != null)
{
Console.WriteLine(frisbeefly(obj).ToString());
}
}
~Dog()
{
Console.WriteLine("Dog销毁");
}
 public class Person
{
public string name { get; set; }
public Pet pet { get; set; }
public void orderPet(string type)
{
Console.WriteLine("start order");
if (type.ToLower() == "bark")
{
Console.WriteLine(string.Format("I`m {0},{1} is me", pet.GetType().ToString(), pet.name));
Console.WriteLine(string.Format("time:{0}", DateTime.Now.ToString()));
if (pet.name == "旺财")
{
pet.name += "";
}
pet.bark();
}
Console.WriteLine("end order");
}
public object seefrisbee(object color)
{
 return string.Format("{0}看到{1}追着{2}颜色的飞盘", name,pet.name,color);
}
~Person()
{
Console.WriteLine(name + "离开");
}
}
  <object id="person" type="SpringDemo.Person,SpringDemo" singleton="true" >
<property name="pet" ref="dog" ></property>
<property name="name" value="Yahue"></property>
<listener event="frisbeefly" method="seefrisbee">
<ref object="dog"/>
</listener>
</object>
<object id="dog" type="SpringDemo.Dog,SpringDemo" singleton="true">
<property name="name" value="旺财"></property>
<replaced-method name="run" replacer="manyrun" >
<arg-type match="String"/>
</replaced-method> </object>
<object id="manyrun" type="SpringDemo.ManyRun,SpringDemo"></object>

调用代码:

  

  Dog dog = ctx.GetObject("dog") as Dog;
dog.chasefrisbee("红色");

执行结果:

  

spring.net (3)依赖注入基础的更多相关文章

  1. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转

    Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...

  2. 谈谈自己了解的spring.NET的依赖注入

         spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...

  3. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  4. Spring学习(一)——Spring中的依赖注入简介【转】

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  5. spring六种种依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  6. spring.NET的依赖注入

    谈谈自己了解的spring.NET的依赖注入   spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection), ...

  7. spring 四种依赖注入方式以及注解注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  8. JavaEE开发之Spring中的依赖注入与AOP

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  9. JavaEE开发之Spring中的依赖注入与AOP编程

    上篇博客我们系统的聊了<JavaEE开发之基于Eclipse的环境搭建以及Maven Web App的创建>,并在之前的博客中我们聊了依赖注入的相关东西,并且使用Objective-C的R ...

  10. Spring学习(一)——Spring中的依赖注入简介

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

随机推荐

  1. Azure China (2) Azure China管理界面初探

    <Windows Azure Platform 系列文章目录> 首先是Q&A时间 1.我在Azure Global拥有测试账号或者免费的MSDN订阅账号,这个账号可以在国内Azur ...

  2. Windows Azure Virtual Network (11) 创建VNet-to-VNet的连接

    <Windows Azure Platform 系列文章目录> 我们知道,Azure Virtual Network可以 1.将对台Azure VM加入到同一个网段里,同时绑定内网IP地址 ...

  3. Elasticsearch——分词器对String的作用

    更多内容参考:Elasticsearch学习总结 关于String类型--分词与不分词 在Elasticsearch中String是最基本的数据类型,如果不是数字或者标准格式的日期等这种很明显的类型, ...

  4. 浅谈Jquery中的bind(),live(),delegate(),on()绑定事件方式

    前言 因为项目中经常会有利用jquery操作dom元素的增删操作,所以会涉及到dom元素的绑定事件方式,简单的归纳一下bind,live,delegate,on的区别,以便以后查阅,也希望该文章日后能 ...

  5. LeetCode - 207. Course Schedule

    207. Course Schedule Problem's Link ---------------------------------------------------------------- ...

  6. 我所理解的readonly和const

    最近要给学校软件小组新成员讲几次课,所以把很多以前懒得学习的和模糊不清的知识点,重新学习了一下. MSDN是这样解释的: readonly 关键字与 const 关键字不同. const 字段只能在该 ...

  7. javascript学习笔记2-typeof、Number类型、Boolean()

    1.typeof操作符 对一个值使用typeof操作符可能返回下列某个字符串 "undefined"——这个值未定义 "boolean"——这个值是布尔值 &q ...

  8. DataGridView修改HeaderText

    dataGridView_htList为一个 DataGridView(ht为HoverTree的缩写)方法一:dataGridView_htList.Columns["HtAddTime& ...

  9. UWP游戏防内存修改器的方法

    最近我一直在编写适用于Windows 10商店的游戏.这款游戏比较怕玩家用修改器改金钱,因为这种修改会导致某些内购失效并且损害公平性.于是我把自己见过的三种反修改器的方法给网友们介绍一下. 首先说明一 ...

  10. Extjs中全键盘操作,回车跳到下一单元格

    listeners: { afterRender: function (thisForm, options) { var els = Ext.DomQuery.select('input[type!= ...