关于测试核心控制器500错误

关于测试核心控制器500错误

Message Servlet.init() for servlet [Ddispatcher] threw exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet.init() for servlet [Ddispatcher] threw exception
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.lang.Thread.run(Thread.java:745)
Root Cause
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'row' for bean class [com.mysql.cj.xdevapi.Row] conflicts with existing, non-compatible bean definition of same name and class [com.mysql.cj.result.Row]
	org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:320)
	org.mybatis.spring.mapper.ClassPathMapperScanner.checkCandidate(ClassPathMapperScanner.java:256)
	org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:259)
	org.mybatis.spring.mapper.ClassPathMapperScanner.doScan(ClassPathMapperScanner.java:181)
	org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:226)
	org.mybatis.spring.mapper.MapperScannerConfigurer.postProcessBeanDefinitionRegistry(MapperScannerConfigurer.java:356)
	org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:123)
	org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:678)
	org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:520)
	org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
	org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
	com.controller.GrobalController.init(GrobalController.java:22)
	javax.servlet.GenericServlet.init(GenericServlet.java:158)
	org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
	org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
	org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803)
	org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:790)
	org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459)
	org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	java.lang.Thread.run(Thread.java:745)
Note The full stack trace of the root cause is available in the server logs.

我的核心控制器
package com.controller;

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

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;

@Controller
public class GrobalController extends GenericServlet {

  private ApplicationContext applicationContext;
    public void init() throws ServletException {
        applicationContext= new ClassPathXmlApplicationContext("spring.xml");
    }


    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //.do
        //  /Staff/add.do
        //  /login.do Test
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;

        String path=request.getServletPath();
        String classname;
        String methodName;
        int index=path.lastIndexOf("/");
        if(index!=-1){
            classname=path.substring(1,index)+"Controller";
            methodName=path.substring(index+1,path.indexOf(".do"));
        } else {
            classname="DefaultController";
            methodName=path.substring(0,path.indexOf(".do"));
        }
        Object obj = applicationContext.getBean(classname);
        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();
        }
    }
}

看错误是发生在init方法好像,,又说org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'row' for bean class [com.mysql.cj.xdevapi.Row] conflicts with existing, non-compatible bean definition of same name and class [com.mysql.cj.result.Row]
请老师帮忙看一下


正在回答

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

2回答

同学你好!

  1. 同学的代码在老师这里没有报这个错,同学的报错信息是说row这个bean重复了,但是提示的两个类都是mysql中的问题,不是同学的代码问题,因此老师推测可能第同学的spring.xml中配置有误

  2. 建议同学使用老师源码中的配置,或者将自己的spring.xml文件贴出来老师在本地测试一下。注意:在我要回答中贴出

  3. 另外同学的代码,有个地方拼写错了呢

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

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

  • 慕神0034598 提问者 #1
    老师,很感谢,,我自己太粗心了没发现小细节的问题,,已经改好啦
    2019-09-29 15:57:41
提问者 慕神0034598 2019-09-29 15:36:07
<?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/sm_personManagement?useUnicode=true&amp;charactrEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=UTC&amp;allowPublicKeyRetrieval=true"/>
        <property name="username" value="root"/>
        <property name="password" value="root"/>
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <!--指定事务管理器-->
        <property name="transactionManager" ref="transactionManager"/>
    </bean>

    <!--开启注解扫描=======================-->
    <context:component-scan base-package="com"/>
</beans>

GrobalController

package com.controller;

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

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;

@Controller
public class GrobalController extends GenericServlet {

  private ApplicationContext applicationContext;
    public void init() throws ServletException {
        applicationContext= new ClassPathXmlApplicationContext("springs.xml");
    }


    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        //.do
        //  Staff/add.do
        //  login.do Test
        HttpServletRequest request=(HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;

        String path=request.getServletPath().substring(1);
        System.out.println(path);
        String classname;
        String methodName;
        int index=path.lastIndexOf("/");
        if(index!=-1){
            classname=path.substring(0,index)+"Controller";
            methodName=path.substring(index,path.indexOf(".do"));
        } else {
            classname="DefaultController";
            methodName=path.substring(0,path.indexOf(".do"));
        }
        Object obj = applicationContext.getBean(classname);
        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();
        }
    }
}

测试控制器 TestController

package com.controller;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("testController")
public class TestController {
    public void show(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("msg","success");
        request.getRequestDispatcher("../index.jsp");
    }}

上面是我原本的spring.xml,,我替换了老师的spring文件,,tomcat配置的路径是

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

进去之后是空白页,,没有跳转,控制台也没有爆红

  • 提问者 慕神0034598 #1
    GrobalController,,里的springs.xml是我替换老师的配置文件,,并不是写错哈
    2019-09-29 15:39:54
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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