繁体   English   中英

春天不知道的豆

[英]Bean unknown with spring

在3.2春季项目中,我在启动应用程序时遇到问题。 系统似乎无法识别某些bean。

我收到此错误:

   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" />

我该如何解决?

@Component
public interface ReportFacade{
    ...
}

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

所以做下面的事情

  public interface ReportFacade{
        ...
    }

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

然后

@Autowired
private ReportFacade reportFacade;

将注入其实现程序ReportFacadeImpl

确保springconfig文件中的componentscan具有正确的接口和类包条目。

您尚未为ReportFacade接口定义实现。

春天不是魔术。 它无法读懂你的豆子应该做什么。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM