今天在跑项目时出现
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean found for dependency [com.chen.service.UserService]: expected at least 1 bean which qualifies as autowire candidate.
原因如上所述,在需要注入某个bean的时候在,但在容器中却找不到bean
首先看你的需要注入的bean中是否加上了注解,并且正确被扫描到了
在确保已经配置了扫描包的位置和使用了正确的注解后依赖出现错误,请检查你的applicationContext.xml
是否配置了ContextLoaderListener类
ContextLoaderListenerl类的作用: 此监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
如果你未配置此监听器,那么ApplicationContex.xmlt中扫描的包中的注解就失效,或者可以说ApplicationContxt.xml中的配置都不能生效,你的注解也就失效,也就不能正确装配bean到容器
容器中找不到你想要的Bean,那肯定抱怨,就报错呗,也就是上面的异常