程序猿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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题