自动装配失败,springmvc注入service失败

controller层

@RestController
public class StudentController {
    @Autowired
    @Qualifier("studentService")
    private StudentService studentService;
    //学生登录
    @RequestMapping("/student/login")
    public String login(@RequestParam String sId,@RequestParam String sPwd, Model model){
        Map<String,Object> map = new HashMap<>();
        map.put("sId",Integer.valueOf(sId));
        map.put("sPwd",sPwd);
        Student student = studentService.login(map);
        System.out.println(studentService);
        if (student != null)
            return "sInfo";
        else {
            model.addAttribute("Error",false);
            return "student";
        }
    }
}

配置文件aplicaitonContext

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
    <!--导入其他spring配置文件-->
    <import resource="spring-dao.xml"/>
    <import resource="springmvc-servlet.xml"/>
    <!--context内容-->
    <context:component-scan base-package="com.hoary.service"></context:component-scan>
    <context:annotation-config/>

    <!--dao层的bean-->
    <bean id="studentMapper" class="com.hoary.dao.StudentMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"></property>
    </bean>
    <bean id="managerMapper" class="com.hoary.dao.ManagerMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"></property>
    </bean>
    <bean id="bookMapper" class="com.hoary.dao.BookMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"></property>
    </bean>
    <bean id="recordMapper" class="com.hoary.dao.RecordMapperImpl">
        <property name="sqlSessionTemplate" ref="sqlSession"></property>
    </bean>

    <!--service层的bean-->
    <bean id="studentService" class="com.hoary.service.StudentServiceImpl">
        <property name="recordMapper" ref="recordMapper"></property>
        <property name="studentMapper" ref="studentMapper"></property>
        <property name="bookMapper" ref="bookMapper"></property>
    </bean>
    <bean id="managerService" class="com.hoary.service.ManagerServiceImpl" >
        <property name="bookMapper" ref="bookMapper"></property>
        <property name="studentMapper" ref="studentMapper"></property>
        <property name="managerMapper" ref="managerMapper"></property>
    </bean>

    <bean id="/student" class="com.hoary.controller.StudentController"></bean>
</beans>

我运行时就报错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentService'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.hoary.service.StudentService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=studentService)}

已解决,找到了原因,原来是我配置的DispatcherServlet中的文件是springmvc-servlet.xml,找不到Service bean

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!--spring容器要写父容器aplicationContext.xml-->  
      <param-value>classpath:aplicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

靓仔,你的service有没有开启注入功能?

你给bean加个name属性,或者只使用@Autowired试下,@Qualifier是by-name注入吧。