属性的注入:

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

 <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("红色");

执行结果:

  

最新文章

  1. ubuntu竖屏显示
  2. Java多线程 3 线程同步
  3. Atom安装以及activate-power-mode atom package插件安装
  4. [WCF编程]10.操作:请求/应答操作
  5. U3D笔记11:47 2016/11/30-15:15 2016/12/19
  6. 阻止网页内部滚动条mousewheel事件冒泡
  7. Oracle sys和system用户、sysdba 和sysoper系统权限、sysdba和dba角色的区别
  8. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之纹理Textures
  9. Activity的生命周期及各生命周期方法的作用
  10. Cookie实例,理解cookie
  11. Python 将pdf转换成txt(不处理图片)
  12. Android Service服务
  13. Codeforces Round #316 (Div. 2B) 570B Simple Game 贪心
  14. hdu 4455 Substrings (DP 预处理思路)
  15. ios 获取融云token
  16. 使用STS时遇到的小“麻烦”
  17. 面向对象(this的问题二)
  18. 20175312 2018-2019-2 《Java程序设计》第6周学习总结
  19. Python Numpy-基础教程
  20. JavaScript--事件绑定及深入(26)

热门文章

  1. java类的初始化和对象的创建顺序
  2. 【模式匹配】更快的Boyer-Moore算法
  3. 基于HTML5树组件延迟加载技术实现
  4. d
  5. 使用PhoneGap搭建一个山寨京东APP
  6. 【读书笔记】--SQL基础概念复习
  7. 安装windows系统的installutil
  8. 让sublime实现js控制台(前提是安装了nodejs)
  9. c# 中基类变量指向派生类对象的实例化
  10. jQuery Ajax实现下拉框无刷新联动