java Spring:找不到符合自动装配条件的匹配 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12866050/
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: No matching bean found which qualifies as autowire
提问by stackoverflow
Application-Context is correctly setup!
应用程序上下文设置正确!
Heres my class scenario
这是我的课堂场景
public interface IManager
{
public void doStuff();
}
@Component
public abstract class ManagerAction implements IManager
{
@Async
@Override
public void doStuff()
{
//doing stuff
}
public abstract manageWorker();
}
@Component
public class Working extends ManagerAction
{
@Override
public manageWorker()
{
//some busy code
}
}
@Component
public class NotWorking extends ManagerAction
{
@Override
public manageWorker()
{
//some busy code
}
}
@Service
public class BusinessWorker
{
@Autowire
private IManager manager_;
public void preformTasks()
{
manager_.doStuff();
}
}
Heres my error
这是我的错误
ERROR [main] (ContextLoader.java:307) - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'BusinessWorker': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.B
eanCreationException: Could not autowire field: private com.background.IManager com.background.BusinessWorker.manager_; nested exception is org.springframework.beans.
factory.NoSuchBeanDefinitionException: No matching bean of type [com.background.IManager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for
this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.background.IManager com.background.BusinessWorker.manager_;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.background.IManager] found for dependency: expected at least 1
bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 28 more
Application-Context
应用上下文
<mvc:annotation-driven />
<task:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.background" />
回答by JB Nizet
The error message says it all: you try to autowire an instance of IManager, but two different Spring components implement this interface, so Spring doesn't know which one to autowire. You need to use the @Qualifier annotationto specify which one you want Spring to autowire.
错误消息说明了一切:您尝试自动装配 IManager 的一个实例,但是两个不同的 Spring 组件实现了这个接口,因此 Spring 不知道要自动装配哪一个。您需要使用 @Qualifier 批注来指定您希望 Spring 自动装配哪一个。