Spring原型bean与单例bean和依赖注入相结合。是否只有配置方法? [英] Spring prototype beans in combination with singleton beans and dependency injection. Is there an approach that is configuration only?

查看:175
本文介绍了Spring原型bean与单例bean和依赖注入相结合。是否只有配置方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单例bean,每次调用函数都需要返回对不同(新)原型bean的引用。我能想到的唯一方法是通过调用其getBean()方法以编程方式从BeanFactory / ApplicatioContext中检索新的原型bean实例。代码示例将跟随...

I have a singleton bean which needs for each call of a function to return a reference to a different (new) prototype bean. The only way that I can think of doing this is to programmatically retrieve a new prototype bean instance from the BeanFactory/ApplicatioContext by invoking its getBean() method. Code sample will follow...

有更好的方法吗?只有通过配置,希望? (我个人怀疑有......)

Is there a better way to do this? Only via configuration, hopefully? (Personally, I doubt there is...)

<bean id="protoBean" scope="prototype"
        class="com.blahblah.ProtoBean" />

<bean id="singletonBean"
        class="com.blahblah.SingletonBean" />

public class ProtoBean {

    ....
}

public class SingletonBean {

    private BeanFactory factory;

    public ProtoBean dispense() {
        return (ProtoBean) factory.getBean("protoBean");
    }

    ....
}


推荐答案

看看方法注射

这篇关于Spring原型bean与单例bean和依赖注入相结合。是否只有配置方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆