豆与春天不为人知

时间:2014-05-27 18:20:49

标签: java spring spring-mvc

在Spring 3.2项目中,我在启动应用程序时遇到问题。 有些豆似乎没有被系统识别。

我收到此错误:

   27-May-2014 12:41:05.879 SEVERE [http-nio-8084-exec-5] org.apache.catalina.core.StandardContext.listenerStart Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
  org.springframework.beans.factory.BeanCreationException: Error creating bean with  name 'reportResource': Injection of autowired dependencies failed; nested exception is  org.springframework.beans.factory.BeanCreationException: Could not autowire field: private  com.prjx.domain.ReportFacade com.prjx.web.resources.ReportResource.reportFacade; nested  exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying  bean of type [com.prjx.domain.facade.ReportFacade] 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)}


@Controller
public class ReportResource
{
  @Autowired
  private UserFacade userFacade;

  @Autowired
  private ReportFacade reportFacade;
...
}


@Component
public interface ReportFacade{
    ...
}

 public class ReportFacadeImpl implements ReportFacade
{
   ...
}

在我的application-context.xml中,我有

<context:component-scan base-package="com.prjx" />

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

@Component
public interface ReportFacade{
    ...
}

永远不会注入依赖项,因为它是一个接口。

所以做下面的事情

  public interface ReportFacade{
        ...
    }

@Component
  public class ReportFacadeImpl implements ReportFacade{
        ...
    }

然后

@Autowired
private ReportFacade reportFacade;

将注入其实现者ReportFacadeImpl

确保spring config文件中的组件可以正确地包含接口和类的包条目。

答案 1 :(得分:1)

您还没有为ReportFacade界面定义实现。

春天没有魔力。它无法读懂你的大脑以了解豆子应该做什么。

因此,您需要创建ReportFacade接口的实现,将其放在Spring扫描的包中,并使用@Component注释该实现。界面本身不应该有@Component注释。