弹簧自动装配和原型范围 [英] Spring autowire and prototype scope
问题描述
我有一个名为酒吧与以下标注类: @Configurable(自动装配= Autowire.BY_TYPE)
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屋!