在自学spring mvc的时候,是跟着视频学的,所有的操作规程、代码都是和视频一模一样,视频里面的运行起来没有任何问题。但是我却遇到了一个非常奇葩的问题:
报错代码:
Type Exception ReportMessage Servlet.init() for servlet springDispatcherServlet threw exceptionDescription The server encountered an unexpected condition that prevented it from fulfilling the request.Exception
javax.servlet.ServletException: Servlet.init() for servlet springDispatcherServlet threw exceptionorg.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Unknown Source)Root Cause
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalAsyncAnnotationProcessor' defined in org.springframework.scheduling.annotation.ProxyAsyncConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor]: Factory method 'asyncAdvisor' threw exception; nested exception is java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injectedorg.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:697)org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:526)org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)javax.servlet.GenericServlet.init(GenericServlet.java:158)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Unknown Source)Root Cause
org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor]: Factory method 'asyncAdvisor' threw exception; nested exception is java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injectedorg.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189)org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:697)org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:526)org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)javax.servlet.GenericServlet.init(GenericServlet.java:158)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Unknown Source)Root Cause
java.lang.IllegalArgumentException: @EnableAsync annotation metadata was not injectedorg.springframework.util.Assert.notNull(Assert.java:115)org.springframework.scheduling.annotation.ProxyAsyncConfiguration.asyncAdvisor(ProxyAsyncConfiguration.java:46)org.springframework.scheduling.annotation.ProxyAsyncConfiguration$$EnhancerBySpringCGLIB$$677d9aae.CGLIB$asyncAdvisor$0(<generated>)org.springframework.scheduling.annotation.ProxyAsyncConfiguration$$EnhancerBySpringCGLIB$$677d9aae$$FastClassBySpringCGLIB$$ae573e37.invoke(<generated>)org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:356)org.springframework.scheduling.annotation.ProxyAsyncConfiguration$$EnhancerBySpringCGLIB$$677d9aae.asyncAdvisor(<generated>)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)java.lang.reflect.Method.invoke(Unknown Source)org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162)org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1123)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1018)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:228)org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:697)org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:526)org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:668)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:634)org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:682)org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:553)org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:494)org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)javax.servlet.GenericServlet.init(GenericServlet.java:158)org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)java.lang.Thread.run(Unknown Source)
经过几番周折,才知道被教学视频挖啦个坑给埋了。
问题就出在springmvc.xml这个配置文件的一段代码里。
一下是教学视频里面的代码段:
<!-- 配置@controller@service--><!-- *表示让所有的包都识别 --><context:component-scan base-package="/"></context:component-scan>
<!-- 配置视图解析器 :逻辑视图、物理视图(***.jsp)视图解析器会将逻辑视图自动转换为物理视图比如物理视图为hello,转化为:hello.jsp--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"></property><property name="suffix" value=".jsp"></property></bean>
我们要做的就是把其中的“<context:component-scan base-package="/"></context:component-scan>” base-package里面的/改为自己的包名即可(原因就是用/的时候,表示所有的包都要被识别,这样做的结果就是mvc自带的包也一起被扫描了)。
一下是我改后的:
<context:component-scan base-package="com.springmvc.handler"></context:component-scan>//如果有多个包名,就用逗号隔开
如果是有多个包,就用 ,(逗号)隔开:
<context:component-scan base-package="com.fengye.Handler,com.fengye.pojo,com.fengye.dao"></context:component-scan>
见证效果:
成功!!!