Error creating bean with name ‘xxx‘: Injection of autowired dependencies failed解决办法

完整的报错信息:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'userDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.core.dao.UserDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
报错中异常为NoSuchBeanDefinitionException,指出没有找到要注入的内容(service或dao),从两方面着手:
1、检查下在spring配置中,是否将对应的包加入扫描。
dao类要加上@Repository注解
applicationContext.xml:
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.xxx.dao"/>
</bean>
扫描service包下所有使用注解的类型
<context:component-scan base-package="com.xxx.service"/>
springmvc.xml
<!-- 扫描包 -->
<context:component-scan base-package="com.core.service"/>
<!-- 扫描包 -->
<context:component-scan base-package="com.core.controller" />
2、service类可能没加@service
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
}
3、web.xml 里没有配置监听器,导致配置文件没加载 博主犯得错误 -_-||
<!-- 配置监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
4、被@Autowired标注的属性不能是静态变量 (基础的知识,但容易忘记)




更多推荐
所有评论(0)