1.spring.net Look-up Method 查找方法的注入(方法是抽象的需要spring.net注入)

1.为什么需要查找方法的注入
当Object依赖另一个生命周期不同的Object,尤其是当singleton依赖一个non-singleton时,常会遇到不少问题,Lookup Method Injection正是对付这些问题而出现的,在上述情况中,setter和构造注入都会导致singleton去维护一个non-singleton bean的单个实例,某些情况下,我们希望让singleton Object每次要求获得bean时候都返回一个non-singleton bean的新实例 

当一个singleton Object A 在每次方法调用的时候都需要一个non-singleton Object B,此时就会产生这样一个问题,因为A为singleton,所以容器只会创建一次A,那么也只有一次机会来创建A的属性,Bean B也只能被初始化一次,但是我们调用每个方法的时候又都需要一个新的B的实例。通常的时候我们只要new一个就可以,但在Spring中这不符合整体性的设计,这样就有了方法注入。
2.代码
2.1
using System.Collections;
namespace Fiona.Apple
{
 public abstract class CommandManager
 {
 public object Process(IDictionary commandState)
 {
 Command command = CreateCommand();
 command.State = commandState;
 return command.Execute();
 }
 // okay... but where is the implementation of this method?
 protected abstract Command CreateCommand();
 }
}

2.2
<!-- a stateful object deployed as a prototype (non-singleton) -->
<object id="command" class="Fiona.Apple.AsyncCommand, Fiona" singleton="false">
 <!-- inject dependencies here as required -->
</object>
<!-- commandProcessor uses a statefulCommandHelpder -->
<object id="commandManager" type="Fiona.Apple.CommandManager, Fiona">
 <lookup-method name="CreateCommand" object="command"/>
The IoC container
Spring Framework (Version 1.3.2) 46
</object>
原文地址:https://www.cnblogs.com/kexb/p/5919361.html