spring.net (3)依赖注入基础

属性的注入:

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

 <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标签作为标识。同样具有于属性注入相同的方式,使用namerefvalue作为构造器注入的属性,如下:

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

集合类型的注入:

  IDictionary类型

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

同理,<dictionary>元素key-typevalue-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[0];
            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节点处配置eventmethod属性指明事件名和绑定的方法,并增加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 += "1";
                }
                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("红色");

执行结果:

  

原文地址:https://www.cnblogs.com/yahue/p/3509032.html