Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException

本文介绍了一个关于 Spring MVC 单元测试中出现 NoSuchBeanDefinitionException 的问题及解决方案。错误出现在尝试注入 controller.UserController 时,由于 applicationContext.xml 中的 context:component-scan 配置不当导致。

开发框架:Spring4+Mybatis3+Jta(atomikos)

运行单元测试报以下错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'controller.UserController' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

最终排查发现是<context:component-scan/>的原因导致。

  1.   项目结构:
    141750_Aqe5_2289161.png
  2. applicationContext.xml配置信息:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-4.3.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-4.3.xsd" default-autowire="byName">
    
        <!-- 1.扫描注解-->
        <context:component-scan base-package="controller.*"></context:component-scan>
        <context:component-scan base-package="mapper.*"></context:component-scan>
        <context:component-scan base-package="model.*"></context:component-scan>
        <context:component-scan base-package="service.impl"></context:component-scan>
        <!--2.注解驱动-->
        <mvc:annotation-driven></mvc:annotation-driven>
    </beans>
    base-package="controller.*"有误,应为base-package="controller"

转载于:https://my.oschina.net/u/2289161/blog/822082

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值