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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值