簡體   English   中英

春季:未找到符合自動裝配條件的匹配豆

[英]Spring: No matching bean found which qualifies as autowire

應用程序上下文已正確設置!

這是我班的情景

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();
  }
}

這是我的錯誤

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

應用上下文

<mvc:annotation-driven />
<task:annotation-driven />
<context:annotation-config />
<context:component-scan base-package="com.background" />

錯誤消息說明了一切:您嘗試自動裝配IManager實例,但是兩個不同的Spring組件實現了此接口,因此Spring不知道要自動裝配哪個。 您需要使用@Qualifier批注指定您要Spring自動裝配的那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM