程序猿Halley 2021-08-05 17:16 采纳率: 100%
浏览 100
已结题

自动装配失败,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条回答 默认 最新

  • 程序猿Halley 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日

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加