测试核心控制器 500错误
错误信息
Type 异常报告
消息 No bean named 'selfController' is defined
描述 服务器遇到一个意外的情况,阻止它完成请求。
Exception
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'selfController' 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:48)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
com.imooc.sm.global.EncodingFilter.doFilter(EncodingFilter.java:18)
Note 主要问题的全部 stack 信息可以在 server logs 里查看
-----------------------------------------------------------------------------------------------------
首先执行的是show.do,错误之后我在控制器类里加输出beanName和mothodName,控制台显示
Connected to server
[2019-10-17 11:30:52,300] Artifact sm_web:war: Artifact is being deployed, please wait...
17-Oct-2019 11:30:53.336 信息 [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。
17-Oct-2019 11:30:53.536 警告 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [178] milliseconds.
[2019-10-17 11:30:53,559] Artifact sm_web:war: Artifact is deployed successfully
[2019-10-17 11:30:53,559] Artifact sm_web:war: Deploy took 1,259 milliseconds
17-Oct-2019 11:31:00.493 信息 [http-nio-8080-exec-5] org.springframework.context.support.ClassPathXmlApplicationContext.prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3f933e83: startup date [Thu Oct 17 11:31:00 CST 2019]; root of context hierarchy
17-Oct-2019 11:31:00.534 信息 [http-nio-8080-exec-5] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions Loading XML bean definitions from class path resource [spring.xml]
17-Oct-2019 11:31:00.832 警告 [http-nio-8080-exec-5] org.mybatis.spring.mapper.ClassPathMapperScanner.doScan No MyBatis mapper was found in '[com.imooc.sm.dao]' package. Please check your configuration.
17-Oct-2019 11:31:00.897 信息 [http-nio-8080-exec-5] org.springframework.jdbc.datasource.DriverManagerDataSource.setDriverClassName Loaded JDBC driver: com.mysql.cj.jdbc.Driver
beanName:selfController
mothodName:show
17-Oct-2019 11:31:02.187 信息 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory 把web 应用程序部署到目录 [D:\MYSDE\apache-tomcat-9.0.24\webapps\manager]
17-Oct-2019 11:31:02.211 信息 [Catalina-utility-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [D:\MYSDE\apache-tomcat-9.0.24\webapps\manager] has finished in [24] ms
----------------------------------------------------------------------------------------------------------
以下是我的spring.xml
<?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.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sm?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="clown3rd"/>
</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>
-----------------------------------------------------------------------------------------
以下是核心控制器类的代码
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");
}
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"));
}
System.out.println("beanName:" + beanName);
System.out.println("mothodName:" + methodName);
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();
}
}
}
正在回答
同学你好!
这里其实不算错误,目前项目运行show.do报这个错误是正常的呢,这是因为:
访问show.do,这个请求会进入核心控制器中,目前核心控制器中的逻辑默认会访问selfController这个bean,但是我们的项目中没有写这个bean,所以就报selfController这个bean找不到了。
所以同学这里只要能够正常运行show.jsp,就是正确的。
如果我的回答解决了你的疑惑,请采纳,祝学习愉快~
- 参与学习 人
- 提交作业 323 份
- 解答问题 8263 个
本阶段将带你学习主流框架SSM,以及SpringBoot ,打通成为Java工程师的最后一公里!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星