No bean named 'departmentController' available?

No bean named 'departmentController' available?

25-Feb-2019 22:48:47.848 严重 [http-nio-8080-exec-1] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Global] in context with path [/ssm] threw exception

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'departmentController' available

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:775)

at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)

at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)

at com.imooc.global.DispatcherServlet.service(DispatcherServlet.java:47)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at com.imooc.global.EncodingFilter.doFilter(EncodingFilter.java:20)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)


25-Feb-2019 22:48:48.360 严重 [http-nio-8080-exec-3] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [Global] in context with path [/ssm] threw exception

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'departmentController' available

at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:775)

at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1221)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:294)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)

at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1105)

at com.imooc.global.DispatcherServlet.service(DispatcherServlet.java:47)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at com.imooc.global.EncodingFilter.doFilter(EncodingFilter.java:20)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)

at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

at java.lang.Thread.run(Thread.java:748)


package com.imooc.controller;

import com.imooc.sm.detity.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 {
       System.out.println("controller");
       List<Department> list = departmentService.getAll();
       request.setAttribute("LIST",list);
       request.getRequestDispatcher("../department_list.jsp").forward(request,response);
   }
}

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.4//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.sm.dao.DepartmentDao">
    <resultMap id="resultMap" type="Department">
        <result property="id" column="id" javaType="Integer"/>
        <result property="address" column="address" javaType="String"/>
    </resultMap>

    <insert id="insert" parameterType="Department" useGeneratedKeys="true">
        insert into department(name,address) values (#{name},#{address})
    </insert>
    
    <delete id="delete" parameterType="Integer">
        delete from department where id = #{id}
    </delete>

    <update id="update" parameterType="Department">
        update department set name = #{name},address = #{address} where id = #{id}
    </update>

    <select id="selectById" parameterType="Integer" resultMap="resultMap">
        select * from department where id = #{id}
    </select>

    <select id="selectAll" resultMap="resultMap">
        select * from department
    </select>

</mapper>
package com.imooc.sm.service;

import com.imooc.sm.dao.DepartmentDao;
import com.imooc.sm.detity.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("departmentService")
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;

    public void add(Department department) {
        departmentDao.insert(department);
    }

    public void remove(Integer id) {
        departmentDao.delete(id);
    }

    public void edit(Department department) {
        departmentDao.update(department);
    }

    public Department get(Integer id) {
        return departmentDao.selectById(id);
    }

    public List<Department> getAll() {
        return departmentDao.selectAll();
    }
}

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

       //  /staff/add.do     login.do
       //  staffController
       //   public void add(HttpServletRequest request,HttpServletResponse response)

       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"));
       }

       Object obj = context.getBean(beanName);

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

   }
}

正在回答

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

5回答

看到同学的同学的controller是在com.imooc.controller包下是吗?

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

建议把包路径改成:com.imooc.sm.controller 试试,因为同学在配置文件中就是扫描的这个包。有可能是controller没有被注解扫描到从而没有交给spring

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

提问者 慕粉4103086 2019-02-27 12:13:20

http://img1.sycdn.imooc.com//climg/5c760e560001370404950214.jpgspring文件也没有问题

提问者 慕粉4103086 2019-02-26 16:27:08

<?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"/>
       <property name="username" value="root"/>
       <property name="password" value=""/>
   </bean>

   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="typeAliasesPackage" value="com.imooc.sm.entity"/>
   </bean>

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.imooc.sm.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.sm.service.*.*(..))"/>
       <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
   </aop:config>

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

  • 配置代码写的也是没有问题,还有一个可能是,你的spring.xml文件有没有放到resource文件夹中呢?如果没有建议你放到resource文件中试试。祝学习愉快~
    2019-02-26 19:21:02
提问者 慕粉4103086 2019-02-26 11:46:27
chrismorgen 2019-02-26 11:20:00

同学的代码没有问题,请问同学使用的jdk版本是多少呢?另外建议你按照下图操作并重新运行试试。祝学习愉快~

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

  • 提问者 慕粉4103086 #1
    jdk的版本有问题吗?着急啊。这个问题 试过了重建工程,错误提示还是一样的
    2019-02-26 11:49:21
  • chrismorgen 回复 提问者 慕粉4103086 #2
    你好同学,测试了你的代码是没有问题的,建议你贴一下你的spring.xml配置文件的代码,祝学习愉快~
    2019-02-26 16:25:35
  • 提问者 慕粉4103086 回复 chrismorgen #3
    好的。现在就贴出来
    2019-02-26 16:26:28
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

0 星
SSM主流框架入门与综合项目实战2018版
  • 参与学习           人
  • 提交作业       205    份
  • 解答问题       4317    个

Java中非常实用的SSM整合开发内容,从Spring开始,到MyBaits的进阶内容,再到SpringMVC的应用,最后是SSM整合开发案例,逐步深入,助你成长为一名Java工程师!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

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