spring 自动装配带有@Component 注释的非托管 Bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1004278/
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
Autowiring Unmanaged Beans Annotated With @Component
提问by Alex Beardsley
I want to use @AutoWired to inject a non-managed bean configured with @Component into a managed bean. I'm pretty sure I have the configuration right, but for some reason I keep getting the exception:
我想使用@AutoWired 将配置了@Component 的非托管bean 注入托管bean。我很确定我的配置是正确的,但由于某种原因,我不断收到异常:
No unique bean of type [foo.Baz] is defined: Unsatisfied dependency of type [class foo.Baz]: expected at least 1 matching bean
Based on the error, I'm guessing it's not able to find the Baz class, but I'm not sure why. It's my understanding that the context:spring-configured element in the XML config was supposed to allow me to do this. I also made sure to include the appropriate jar files (spring-weaving.jar and aspectjweaver.jar).
根据错误,我猜它无法找到 Baz 类,但我不确定为什么。我的理解是 XML 配置中的 context:spring-configured 元素应该允许我这样做。我还确保包含适当的 jar 文件(spring-weaving.jar 和 aspectjweaver.jar)。
Here's a simple example of my set up.
这是我的设置的一个简单示例。
My XML config:
我的 XML 配置:
<beans ...>
...
<context:annotation-config/>
<context:spring-configured/>
<context:component-scan base-package="foo"/>
<bean id="bar" class="foo.Bar"/>
...
</beans>
I have one managed bean:
我有一个托管 bean:
package foo;
public class Bar {
@Autowired
private Baz baz;
public void setBaz(Baz baz) {
this.baz = baz;
}
...
}
And one unmanaged bean:
还有一个非托管 bean:
package foo;
@Component
public class Baz {
...
}
Is there something I'm missing?
有什么我想念的吗?
EDIT: The log lists the beans its instantiating, and foo.Baz isn't one of them. I don't know why it's not picking up the @Component annotated class.
编辑:日志列出了它实例化的 bean,而 foo.Baz 不是其中之一。我不知道为什么它没有选择 @Component 注释类。
回答by Michael Wiles
Because Bar is configured with xml, it can only be configured with xml. i.e. you can't mix them. So that "@Autowired" annotation on Baz is not getting picked up (none of the annotations would be). It is only when you add the spring annotation at class level that spring will listen to any of the other annotations.
因为Bar是用xml配置的,所以只能用xml配置。即你不能混合它们。所以 Baz 上的“@Autowired”注释没有被选中(没有一个注释会被选中)。只有当您在类级别添加 spring 注释时,spring 才会侦听任何其他注释。
What you'll need to do is in the xml configure the bean to be autowired by type, add a setter for that type and you'll achieve the desired behaviour.
您需要做的是在 xml 中将 bean 配置为按类型自动装配,为该类型添加一个 setter,您将实现所需的行为。
<bean id="bar" class="foo.Bar" autowire="byType"/>
One more thing, when you annotate a bean with @Component it isa spring managed bean. Just because it is not created with xml does not mean it is unmanaged. An unmanaged bean is one you don't get from spring.
还有一件事,当你用 @Component 注释一个 bean 时,它是一个 spring 管理的 bean。仅仅因为它不是用 xml 创建的并不意味着它是不受管理的。非托管 bean 是 Spring 所没有的。
Bar and Baz are both spring managed. It is the mechanism you've chosen to define them that differs.
Bar 和 Baz 都是春季管理的。不同的是您选择的定义它们的机制。
回答by Jon Vaughan
The previous response is not correct, in one aspect. You can autowire beans that are otherwise configured with xml.
一方面,先前的回应是不正确的。您可以自动装配以其他方式使用 xml 配置的 bean。
From section 3.4.5 in http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html:
来自http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html 中的第 3.4.5 节:
When using XML-based configuration metadata[2], you specify autowire mode for a bean definition with the autowire attribute of the element. The autowiring functionality has five modes. You specify autowiring per bean and thus can choose which ones to autowire.
使用基于 XML 的配置元数据 [2] 时,您可以使用元素的 autowire 属性为 bean 定义指定自动装配模式。自动装配功能有五种模式。您为每个 bean 指定自动装配,因此可以选择要自动装配的那些。
You can autowire by name, type and constructor. There is a crude example of this here: http://www.java2s.com/Code/Java/Spring/AutoWiring.htm
您可以按名称、类型和构造函数自动装配。这里有一个粗略的例子:http: //www.java2s.com/Code/Java/Spring/AutoWiring.htm
回答by Vishal Makwana
The error is due to the sequence of beans defined in your XML config file.
该错误是由于 XML 配置文件中定义的 bean 序列造成的。
As on your XML file, bean for baris created first and then their dependent beans due to which @Autowiredis not able to find baz.
与您的 XML 文件一样,bar首先创建bean for ,然后创建它们的依赖 bean,因为@Autowired无法找到baz.
You must first declare bean for bazand then for bar.
您必须首先声明 bean forbaz然后 for bar。

