HalleyCoder 2021-08-05 17:16 采纳率: 100%
浏览 104
已结题

自动装配失败,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)}

  • 写回答

3条回答 默认 最新

  • HalleyCoder 2021-08-06 15:47
    关注

    已解决,找到了原因,原来是我配置的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>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 8月14日
  • 已采纳回答 8月6日
  • 创建了问题 8月5日

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!