No bean named 'smController' is defined

No bean named 'smController' is defined

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

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

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

访问路径是:http://localhost:8080/sm/department/list.do

package com.imooc.sm.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;

public void init()throws ServletException{
    super.init();
    context=new ClassPathXmlApplicationContext("spring.xml");
}


    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)res;

        /*
        *
         */
        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(index+1,path.indexOf(".do"));
        }

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

    }
}
package com.imooc.sm.controller;

import com.imooc.sm.entity.Department;
import com.imooc.sm.service.DepartmentService;
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;
import java.util.List;

@Controller("departmentController")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    //  /department/list.do    /department_list.jsp
    public void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Department> list=departmentService.getAll();
        request.setAttribute("LIST",list);
        request.getRequestDispatcher("../department_list.jsp").forward(request,response);

    }
}
<?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">

    <!--Spring 整合Mybatis-->
    <!--数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/sm?useUnicode=true&amp;charaterEncoding=UTF-8"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    <!--session工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.imooc.sm.entity"></property>
    </bean>
    <!--持久化对象-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imooc.sm.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!--声明式事务-->
    <!--事务管理器-->
    <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--通知-->
    <tx:advice id="txAdvice" transaction-manager="transcationManager">
        <tx:attributes>
            <tx:method name="get*" read-only="true"/>
            <tx:method name="fine*" 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.sm.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

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


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

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

4回答
好帮手慕小脸 2020-06-11 16:46:06

同学你好,同学控制台是否输出了beanName和methodName呢?另外建议同学打上断点,使用debug试试。如下所示:

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

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


祝学习愉快~


好帮手慕小脸 2020-06-11 11:14:57

同学你好,在DispatcherServlet中输出beanName和methodName。如下所示的位置处:

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

然后执行程序,查看控制台是否输出beanName和methodName,如果有报错,同学将报错信息贴出

祝学习愉快~

  • 提问者 周鸿宁 #1
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'departmentController' is defined org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:638) org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1159) org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:282) org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195) org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:973) com.imooc.sm.global.DispatcherServlet.service(DispatcherServlet.java:46) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) com.imooc.sm.global.EncodingFilter.doFilter(EncodingFilter.java:22)
    2020-06-11 13:37:26
好帮手慕小脸 2020-06-10 19:20:35

同学你好,

1、很抱歉,老师这里没有注意,经核实是有sm的。

2、关于找不到departmentController,建议同学输出一下beanName和methodName,然后查看方法名以及路径是否正确。如果有报错,同学需将控制台的报错贴一下。

祝学习愉快~

  • 提问者 周鸿宁 #1
    请问怎么输出?
    2020-06-11 07:47:14
好帮手慕小脸 2020-06-10 14:42:41

同学你好!错误的含义是smController,没有被定义,但是在项目中没有smController,建议同学这里不要添加sm,直接写为/。并且访问路径中的sm也需要去掉.

注:这里如果加上sm的话,相当于是添加了项目名

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

祝学习愉快~

  • 提问者 周鸿宁 #1
    课程里面老师是有加sm的,你们核实一下
    2020-06-10 18:44:33
  • 提问者 周鸿宁 #2
    而且去掉之后,会显示departmentController找不到
    2020-06-10 18:45:22
  • 提问者 周鸿宁 #3
    那为什么视频中有加sm是可以的呢?是哪里设置不一样呢?
    2020-06-11 01:57:32
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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