Java Spring 自动装配和原型范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9819994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring autowire and prototype scope
提问by Avner Levy
I have a class named Bar with the following annotation:
@Configurable(autowire = Autowire.BY_TYPE)
我有一个名为 Bar 的类,带有以下注释:
@Configurable(autowire = Autowire.BY_TYPE)
On a private member I have the following annotation:
在私人成员上,我有以下注释:
@Autowired(required = true)
private Foo foo;
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:
在 spring 配置中,我有一个 Foo 类的 bean。如果用scope="prototype"
它定义的 bean不起作用,我会收到以下异常:
NoSuchBeanDefinitionException: No matching bean of type Foo found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency
NoSuchBeanDefinitionException: 没有为依赖找到匹配的 Foo 类型的 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者
Once I change the injected bean scope to "singleton"
it works fine.
一旦我将注入的 bean 范围更改为"singleton"
它就可以正常工作。
Isn't auto wiring of prototype scoped bean allowed?
是否允许原型作用域 bean 的自动连接?
Is there any workaround (beside getting the bean manually)?
是否有任何解决方法(除了手动获取 bean)?
Thanks in advance, Avner
提前致谢, Avner
采纳答案by Avner Levy
The following links provide alternative solutions for such scenarios:
以下链接为此类场景提供了替代解决方案:
- http://whyjava.wordpress.com/2010/10/30/spring-scoped-proxy-beans-an-alternative-to-method-injection/
- http://benkiew.wordpress.com/2012/04/22/spring-2-5-x-create-prototype-instances-from-code/
- http://whyjava.wordpress.com/2010/10/30/spring-scoped-proxy-beans-an-alternative-to-method-injection/
- http://benkiew.wordpress.com/2012/04/22/spring-2-5-x-create-prototype-instances-from-code/
The first link talks about adding to Foo:
第一个链接谈论添加到 Foo:
@Component
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS, value = "prototype")
class Foo
Which will cause a new instance every call.
每次调用都会导致一个新实例。
回答by user1268571
If the injected bean scope is 'Singleton', the same instance of the bean will be auto-wired. If the injected bean scope is 'prototype', new instance will be created as part of auto-wire process.
如果注入的 bean 范围是“Singleton”,则将自动连接 bean 的同一实例。如果注入的 bean 范围是“原型”,则将创建新实例作为自动连接过程的一部分。
What version of Spring you are using and also attach the spring-context.xml for more details.
您使用的是哪个版本的 Spring,并附上 spring-context.xml 以获取更多详细信息。
回答by vine
I believe its the prototype/singleton thing declared in your xml for that bean is the issue.
我相信它在您的 xml 中为该 bean 声明的原型/单例事物是问题所在。
Isn't auto wiring of prototype scoped bean allowed?
是否允许原型作用域 bean 的自动连接?
I think it is not allowed. The logic is if it is allowed, then whenever you use that class, then it needs to reinstantiate that bean always as its field. Which is weird especially if the class that this bean is autowired as a field is a singleton itself.
我认为这是不允许的。逻辑是,如果允许,那么无论何时使用该类,它都需要始终将该 bean 重新实例化为它的字段。这很奇怪,特别是如果这个 bean 被自动装配为一个字段的类本身就是一个单例。
is there any workaround (beside getting the bean manually)?
是否有任何解决方法(除了手动获取 bean)?
Just try to remove the scope attribute, cause if it is of prototype attribute, it won't be retrieved. If those beans(services and DAO) are declared in your applicationContext, just let the autowire annotation get it as singleton since by default beans are singleton, which it should be.
只是尝试删除范围属性,因为如果它是原型属性,它将不会被检索。如果这些 bean(服务和 DAO)在您的 applicationContext 中声明,只需让自动装配注释将其作为单例,因为默认 bean 是单例,它应该是单例。