弹簧自动装配和原型范围 [英] Spring autowire and prototype scope

查看:129
本文介绍了弹簧自动装配和原型范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为酒吧与以下标注类:
@Configurable(自动装配= Autowire.BY_TY​​PE)

I have a class named Bar with the following annotation: @Configurable(autowire = Autowire.BY_TYPE)

在一个私有成员,我有以下注释:

On a private member I have the following annotation:

@Autowired(required = true)
private Foo foo;

在Spring的配置我有类Foo的一个bean。如果Bean与定义 =范围原型它不工作,我得到了以下异常:

In the spring configuration I have a bean of class Foo. If the bean is defined with scope="prototype" it doesn't work and I get the following exception:

NoSuchBeanDefinitionException:没有Foo类型匹配豆找到了依赖性:
      预计至少有1 Bean上有资格作为自动装配候选这种依赖

NoSuchBeanDefinitionException: No matching bean of type Foo found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency

有一次,我改变注入豆范围单身它工作正常。

Once I change the injected bean scope to "singleton" it works fine.

是不是自动布线允许范围内的豆?

Isn't auto wiring of prototype scoped bean allowed?

有任何解决方法(手动获取豆旁)?

Is there any workaround (beside getting the bean manually)?

在此先感谢,
阿夫纳

Thanks in advance, Avner

推荐答案

下面的链接提供了这样的场景替代解决方案:

The following links provide alternative solutions for such scenarios:

<一个href=\"http://whyjava.word$p$pss.com/2010/10/30/spring-scoped-proxy-beans-an-alternative-to-method-injection/\">http://whyjava.word$p$pss.com/2010/10/30/spring-scoped-proxy-beans-an-alternative-to-method-injection/

<一个href=\"http://benkiew.word$p$pss.com/2012/04/22/spring-2-5-x-create-prototype-instances-from-$c$c/\">http://benkiew.word$p$pss.com/2012/04/22/spring-2-5-x-create-prototype-instances-from-$c$c/

享受...

这篇关于弹簧自动装配和原型范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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