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

開發框架:Spring4+Mybatis3+Jta(atomikos)spring

運行單元測試報如下錯誤:spring-mvc

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/>的緣由致使。mvc

  1.   項目結構:
  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"
相關文章
相關標籤/搜索