Spring.Net对方法的注入(学习笔记三)

  Spring除了对属性、事件注入外,还可以通过配置实现对方法的注入。这一节说说Spring.Net框架对方法的注入方式。

Spring对方法的注入有两种方式,本节要说明的就是它的两种注入方式:

  1、查询方法注入(lookup method Injection)

  2、替换任意方法注入

  先介绍开发环境及Spring版本: VS2008 SP1。Spring版本:1.3.0。

  1.查询方法注入。

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

  配置方式如下:

 

代码
 
   
1 < objects xmlns = " http://www.springframework.net " xmlns:aop = " http://www.springframework.net/aop " >
2 < object id = " parent " type = " lookUpMethod.Parent,lookUpMethod " singleton = " false " >
3 < lookup - method name = " CreateInstance " object = " children " />
4
5 </ object >
6
7 < object id = " children " type = " lookUpMethod.Children,lookUpMethod " >
8
9 </ object >
10 </ objects >

类代码:

 

代码
 
   
1 public abstract class Parent
2 {
3 // public abstract Children CreateInstance();
4  
5 public virtual Children CreateInstance()
6 {
7 return null ;
8 }
9 }
10 public class Children
11 {
12 public void TestMethod()
13 {
14 Console.WriteLine( " Chileren method " );
15
16 }
17 }

使用方式:

 

代码
 
   
1 static void Main( string [] args)
2 {
3 IApplicationContext context = ContextRegistry.GetContext();
4
5 Parent parent = (Parent)context.GetObject( " parent " );
6 Console.WriteLine(parent.CreateInstance().ToString());
7 parent.CreateInstance().TestMethod();
8 Console.ReadLine();
9
10 }

结果如下图:

 

  由运行的结果我们可以看出:调用parent的抽象方法CreateInstance时,实际上返回了children的类型。

  如果我们将抽象方法CreateInstance增加一个string类型的参数,将方法的签名改成如下方式:

public abstract Children CreateInstance(string str);

使用方式改成:

  Console.WriteLine(parent.CreateInstance("aa").ToString());
       parent.CreateInstance("aa").TestMethod("str");

  则运行出现如下图示异常:

parent中抽象方法、虚方法运行结果是完全一样的。大家有兴趣可以试试。

由以上用法可以总结出以下结论: 

被注入的方法不能带参数。

  2、替换方法注入

  这是一种用的比较少的注入方式。在Spring的配置中,通过replaced-method在需要替换类中指定需要被替换的方法,以及被哪个类替换。替换类应该实现Spring.Objects.Factory.Support中的IMethodReplacer接口。IMethodReplacer接口只有一个object Implement(object target, MethodInfo method, object[] arguments)方法。当我们调用被替换类中相应方法时,会执行Implement方法。当然,我们可以中Implement中,通过参数信息获取到相应的部分信息。

  配置如下:

 

代码
 
   
1 < objects xmlns = " http://www.springframework.net " xmlns:aop = " http://www.springframework.net/aop " >
2 < object id = " parent " type = " ReplaceMethod.Parent,ReplaceMethod " >
3 < replaced - method name = " ParentMethod " replacer = " children " >
4 <!--< arg - type match = "" />-->
5 </ replaced - method >
6 < replaced - method name = " ParentMethod " replacer = " children " >
7 < arg - type match = " String " />
8 </ replaced - method >
9 </ object >
10 < object id = " children " type = " ReplaceMethod.Children,ReplaceMethod " >
11
12 </ object >
13 </ objects >

被替换类的代码如下:

代码
 
   
1 public class Parent
2 {
3 public virtual void ParentMethod()
4 {
5 Console.WriteLine( " parent methods " );
6 }
7
8 public virtual void ParentMethod( string str)
9 {
10 Console.WriteLine( " method with parament: " + str);
11 }
12 }
替换类的代码:

代码
 
   
1 public class Children : IMethodReplacer
2 {
3 public object Implement( object target, MethodInfo method, object [] arguments)
4 {
5 Console.WriteLine( " target type is:{0} " ,target.ToString());
6 Console.WriteLine( " method name is :{0} " ,method.Name);
7 if (arguments.Length > 0 )
8 {
9 Console.WriteLine( " the first element in artmennts is :{0} " , arguments[ 0 ].ToString());
10 }
11 return new object ();
12 }
13
14 }

使用方式:

代码
 
   
1 static void Main( string [] args)
2 {
3 IApplicationContext context = ContextRegistry.GetContext();
4
5 Parent parent = (Parent)context.GetObject( " parent " );
6 parent.ParentMethod();
7
8 parent.ParentMethod( " test str " );
9 Console.ReadLine();
10
11 }
运行结果如下:

 

  由运行结果我们可以得知:上面两行是调用没有参数的函数调用的结果,下面三行是带有参数的函数调用的结果。

       结论:

  1、在替换方法注入中,在被替换类中的具有重载的方法,通过replaced-method节点中的arg-type指定的。

  2、在被替换类中,需要被替换的方法需要是虚方法或者抽象方法(否则异常会通查询方法中不设置为虚方法或抽象方法的异常相同).

  代码下载Spring中的方法注入

  参考文档:Spring.Net框架参考

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值