完整的报错信息:
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标注的属性不能是静态变量 (基础的知识,但容易忘记)

GitHub 加速计划 / de / Dependencies
36
1
下载
A rewrite of the old legacy software "depends.exe" in C# for Windows devs to troubleshoot dll load dependencies issues.
最近提交(Master分支:16 天前 )
1997a400 - 3 年前
2f423539 - 3 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐