原创:idea启动项目的是否报错:java.lang.ClassNotFoundException: javax.servlet.ServletContext

在Intellij IDEA中默认所有 scope 为 provided 的依赖不会被加入到 classpath;

想想配置正确,怎么不能创建窗容器呢

ava.lang.ClassNotFoundException: javax.servlet.ServletContext

2020-03-21 17:33:17,156 WARN [org.springframework.context.support.ClassPathXmlApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcContentNegotiationManager': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.web.accept.ContentNegotiationManagerFactoryBean] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mvcContentNegotiationManager': Lookup method resolution failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.web.accept.ContentNegotiationManagerFactoryBean] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:262)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1198)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1123)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:742)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
    at com.ibaiqi.spider.service.TestDeplication.listRecruits(TestDeplication.java:14)
    at com.ibaiqi.spider.service.TestDeplicationAAA.main(TestDeplicationAAA.java:31)
Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.web.accept.ContentNegotiationManagerFactoryBean] from ClassLoader [sun.misc.Launcher$AppClassLoader@18b4aac2]
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:659)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:556)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:541)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:245)
    ... 15 more
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.getDeclaredMethods(Class.java:1975)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:641)
    ... 18 more
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 22 more
Disconnected from the target VM, address: '127.0.0.1:52084', transport: 'socket'

Process finished with exit code 1

 

异常详情:

这是我遇到的异常控制台截图,如下:

异常解释:

在Intellij IDEA中默认所有 scope 为 provided 的依赖不会被加入到 classpath;

所以在编译器启动 spring boot 项目时,会由于某些依赖加载不到而报 javax.servlet.ServletContext 异常。
解决办法:

方法一、 修改【IDEA】- Edit Configurations 的配置项:

步骤1:菜单栏 -> Run -> Edit Configurations

步骤2:Run/Debug Configurations -> Application -> Configuration -> 勾选:include dependencies with "Provided" scope

 

方法二、修改【pom.xml 】文件中的配置:

步骤1:找到图片中的 “spring-boot-starter-tomcat” 标签,有三种方式可以尝试;

    将 <scope>provided</scope> 注释掉 ;
    <scope> 标签内容provided改成compile:<scope>compile</scope> ;
    <scope>标签内写明具体版本,如:<version>1.5.2.RELEASE</version>;

步骤2:这是关于容器的配置,需要把容器tomcat重新加载一下。
————————————————
版权声明:本文为CSDN博主「田潇文」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44259720/article/details/94602764

 

posted @ 2020-03-21 17:41  码哥之旅  阅读(2049)  评论(0编辑  收藏  举报