No bean named 'SelfController' is defined

No bean named 'SelfController' is defined

具体遇到的问题

No bean named ‘SelfController’ is defined

报错信息的截图

这是项目结构图
图片描述

这是报错图片描述

相关课程内容截图

尝试过的解决思路和结果

spring.xml 没错
核心控制器可以正确输出类名与方法名
类名也没错
注解也没有错

粘贴全部相关代码,切记添加代码注释(请勿截图)

package com.imooc.hospital.global;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class DispatcherServlet extends GenericServlet {
    private ApplicationContext context;

    @Override
    public void init() throws ServletException {
        super.init();
        context = new ClassPathXmlApplicationContext("spring.xml");
    }
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        String path = request.getServletPath().substring(1);
        String beanName = null;
        String methodName = null;
        int index = path.indexOf("/");

        if (index != -1){
            beanName = path.substring(0,index) + "Controller";
            methodName = path.substring(index + 1,path.indexOf(".do"));
        }else {
            beanName = "SelfController";
            methodName = path.substring(0,path.indexOf(".do"));
        }
        System.out.println("beanName=====>"+beanName);
        System.out.println("meethodName======>"+methodName);
        System.out.println();

        Object object = context.getBean(beanName);
        try {
            Method method = object.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
            method.invoke(object,request,response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

///==============================================

package com.imooc.hospital.controller;

import com.imooc.hospital.entity.Users;
import com.imooc.hospital.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller("selfController")
public class SelfController {
    @Autowired
    private UserService userService;

    // toLogin.do
    public void toLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("login.jsp").forward(request,response);
    }

    // login.do
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String userName = request.getParameter("username");
        String password = request.getParameter("password");
        Users users = userService.login(userName,password);
        if (userName == null){
            response.sendRedirect("toLogin.do");
        }else {
            request.getSession().setAttribute("USER",users);
            response.sendRedirect("dept/list.do");
        }
    }

    // logout.do
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getSession().setAttribute("USER",null);
        response.sendRedirect("toLogin.do");

    }
}

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--    数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/hospital?useUnicode=true&amp;characterEncoding=utf-8"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
<!--    session工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.imooc.hospital.entity"/>
    </bean>

<!--    持久化对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imooc.hospital.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
     </bean>

<!--    事务-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="search*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.imooc.hospital.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

<!--    全局自动扫描-->
    <context:component-scan base-package="com.imooc.hospital"/>
    <aop:aspectj-autoproxy/>
</beans>

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
提问者 慕粉1705037409 2020-12-11 11:30:58

课程中的  也是这样啊

好帮手慕阿满 2020-12-11 10:42:16

同学你好,报错是找不到SelfController,如:

http://img1.sycdn.imooc.com//climg/5fd2dc070979912303380057.jpg

注解中是selfController,如:

http://img1.sycdn.imooc.com//climg/5fd2dc3b0943a9be03490074.jpg

大小写对不上造成的报错。

需要将如下的SelfController改为selfController

http://img1.sycdn.imooc.com//climg/5fd2dbf209d241d606010187.jpg

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师