spring Method injection Lookup method injection

2015/07/13 22:43
阅读数 112

首先看下spring官方文档上 Method injection的说明

原文:In most application scenarios, most beans in the container are singletons. When a singleton bean needs to collaborate with another singleton bean, or a non-singleton bean needs to collaborate with another non-singleton bean, you typically handle the dependency by defining one bean as a property of the other. A problem arises when the bean lifecycles are different. Suppose singleton bean A needs to use non-singleton (prototype) bean B, perhaps on each method invocation on A. The container only creates the singleton bean A once, and thus only gets one opportunity to set the properties. The container cannot provide bean A with a new instance of bean B every time one is needed.

大概意思是说:在大多数的应用场景,在容器中的大多数bean是单例。当一个单例bean需要与另一个单例bean合作,或非单例bean需要与另一个非单例bean合作,你通常处理依赖通过定义一个bean中定义一个属性依赖另一个bean。这就有个问题出现了,bean的生命周期是不同的。假设单例bean A需要使用非单例bean B,容器只创建bean A一次,(bean B 生命周期是有限) 但是容器不能每次都提供一个 bean B 给单例A 使用.

原文:

A solution is to forego some inversion of control. You can make bean A aware of the container by implementing the ApplicationContextAware interface, and by making a getBean("B") call to the container ask for (a typically new) bean B instance every time bean A needs it. 

解决方案就是:bean A实现ApplicationContextAware 接口 当时用到bean B时就新建一个

// a class that uses a stateful Command-style class to perform some processing
package fiona.apple;

// Spring-API imports
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class CommandManager implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    public Object process(Map commandState) {
        // grab a new instance of the appropriate Command
        Command command = createCommand();
        // set the state on the (hopefully brand new) Command instance
        command.setState(commandState);
        return command.execute();
    }

    protected Command createCommand() {
        // notice the Spring API dependency!
        return this.applicationContext.getBean("command", Command.class);
    }

    public void setApplicationContext(
            ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

Lookup method injection

接下来说说这个 Lokup method injection
此方式可以在配置的时候就说明依赖的bean B 是谁谁谁的哪个方法创建的如下:
AsyncCommand类相当于===>>> bean B(非单例)
CommandManager类相当与====>>> bean A(单例)
在单例的类中不再使用propeties标签注入,而是使用  lookup-method指定bean是command 方法是 createCommand
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
    <!-- inject dependencies here as required -->
</bean>

<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
    <lookup-method name="createCommand" bean="command"/>
</bean>
java代码如下:
package fiona.apple;

// no more Spring imports!

public abstract class CommandManager {

    public Object process(Object commandState) {
        // grab a new instance of the appropriate Command interface
        Command command = createCommand();
        // set the state on the (hopefully brand new) Command instance
        command.setState(commandState);
        return command.execute();
    }

    // okay... but where is the implementation of this method?
    protected abstract Command createCommand();
}






版权声明:本文为博主原创文章,未经博主允许不得转载。

展开阅读全文
加载中

作者的其它热门文章

打赏
0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部