首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >升级spring boot 2.0.0.RC2异常未设置ServletContext

升级spring boot 2.0.0.RC2异常未设置ServletContext
EN

Stack Overflow用户
提问于 2018-02-25 17:07:35
回答 2查看 12.8K关注 0票数 15

调试后出现的问题是mvc配置类EnableWebMvcConfiguration加载太早,servlet还没有加载。

代码语言:javascript
复制
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:587) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1250) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:502) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda$125/98506158.getObject(Unknown Source) ~[na:na]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.0.RC2.jar:2.0.0.RC2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [spring-boot-2.0.0.RC2.jar:2.0.0.RC2]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:388) [spring-boot-2.0.0.RC2.jar:2.0.0.RC2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.RC2.jar:2.0.0.RC2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RC2.jar:2.0.0.RC2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RC2.jar:2.0.0.RC2]
    at com.manway.BccApplication.main(BccApplication.java:16) [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.0.0.RC2.jar:2.0.0.RC2]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 24 common frames omitted
Caused by: java.lang.IllegalStateException: No ServletContext set
    at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:485) ~[spring-webmvc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$5de64506.CGLIB$resourceHandlerMapping$40(<generated>) ~[spring-boot-autoconfigure-2.0.0.RC2.jar:2.0.0.RC2]
    at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$5de64506$$FastClassBySpringCGLIB$$edc706a5.invoke(<generated>) ~[spring-boot-autoconfigure-2.0.0.RC2.jar:2.0.0.RC2]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration$$EnhancerBySpringCGLIB$$5de64506.resourceHandlerMapping(<generated>) ~[spring-boot-autoconfigure-2.0.0.RC2.jar:2.0.0.RC2]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_40]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_40]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_40]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_40]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 25 common frames omitted
EN

回答 2

Stack Overflow用户

发布于 2018-06-01 05:35:19

“调试后,问题是mvc配置类EnableWebMvcConfiguration加载太早,servlet还没有加载。”

我在这上面花了几个小时。我设法找到了发生这种情况的原因。我的配置被分成了几个文件,并且我在Security config (之前创建的)中创建了一个与MVC相关的bean,强制在使用MVC配置之前使用它。

解决方案是将@Bean实例从安全配置移到MVC配置。我希望它能帮助其他人!

票数 42
EN

Stack Overflow用户

发布于 2020-04-10 20:42:15

我也有同样的"No ServletContext set“问题,我想通过一个简单的演示来了解,这个演示对我很有帮助。因此,我将分享我的 simple 演示,希望它也能对您有所帮助。

注意:

(1)我使用的示例与OP略有不同,但概念和问题是相同的。

(2)我删除了一些与本demo无关的日志。

Spring Boot应用程序简单内容-只有3个类:

(1)用@SpringBootApplication注解的主类BeanLoadingDemoApplication

(2)覆盖一个configure方法的class WebSecurityConfiguration extends WebSecurityConfigurerAdapter

(3)和两个测试bean,一个在独立的@Configuration类中,另一个在(2)中的主@SpringBootApplication类中。

启动应用后的日志如下所示。只需关注日志中部分所在的

INFO o.s.web.context.ContextLoader -根WebApplicationContext:初始化在700毫秒内完成INFO c.example.demo.WebSecurityConfiguration - WebSecurityConfiguration() -构造函数

代码语言:javascript
复制
INFO  c.e.demo.BeanLoadingDemoApplication - Starting BeanLoadingDemoApplication 
INFO  o.s.b.w.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http)
INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 700 ms
INFO  c.example.demo.WebSecurityConfiguration - WebSecurityConfiguration() - Constructor
INFO  com.example.demo.TestBeanConfiguration - #############################
INFO  com.example.demo.TestBeanConfiguration - @Configuration - @Bean - testBean()
INFO  com.example.demo.TestBeanConfiguration - #############################
INFO  c.e.demo.BeanLoadingDemoApplication - #############################
INFO  c.e.demo.BeanLoadingDemoApplication - @SringBootApp - @Bean - testBean2
INFO  c.e.demo.BeanLoadingDemoApplication - #############################
INFO  c.example.demo.WebSecurityConfiguration - =======================================
INFO  c.example.demo.WebSecurityConfiguration - @Configuration - WebSecurityConfiguration - @Override configure
INFO  c.example.demo.WebSecurityConfiguration - =======================================
INFO  c.e.demo.BeanLoadingDemoApplication - Started BeanLoadingDemoApplication in 1.371 seconds (JVM running for 2.326)

到目前为止还好吗?很简单,对吧?现在我添加一个

代码语言:javascript
复制
@Bean
public ServletWebServerFactory servletContainer() { 
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
...
}

在类WebSecurityConfiguration中为Tomcat配置第二个连接器。只需将焦点放在这个部件已经“移动”的地方:

WebSecurityConfiguration() -构造函数

您是否看到构造函数是在启动应用程序之后立即调用的,并且是在根WebApplicationContext初始化之前构造的?到目前为止,问题已经很清楚了。

代码语言:javascript
复制
INFO  c.e.demo.BeanLoadingDemoApplication - Starting BeanLoadingDemoApplication
INFO  c.example.demo.WebSecurityConfiguration - WebSecurityConfiguration() - Constructor
INFO  o.s.b.w.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (http) 8080 (http)
INFO  o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 785 ms
INFO  com.example.demo.TestBeanConfiguration - #############################
INFO  com.example.demo.TestBeanConfiguration - @Configuration - @Bean - testBean()
INFO  com.example.demo.TestBeanConfiguration - #############################
INFO  c.e.demo.BeanLoadingDemoApplication - #############################
INFO  c.e.demo.BeanLoadingDemoApplication - @SringBootApp - @Bean - testBean2
INFO  c.e.demo.BeanLoadingDemoApplication - #############################
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource
Caused by: java.lang.IllegalStateException: No ServletContext set

原因是由于Spring Boot在创建自己的Tomcat之前初始化嵌入的WebApplicationContext,因此它需要在初始化Tomcat之前和在初始化根WebApplicationContext之前配置我通过@Bean 添加到Tomcat的连接器!!但是因为我的@BeanWebSecurityConfiguration class中,所以该类的构造时间比预期的要早得多,因此它不会有ServletContext!!

解决方案是将此

代码语言:javascript
复制
@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    ...
}

@Configuration独立类中,这样WebSecurityConfiguration的构造函数就不会被提前调用,并且通过这样做,它将获得所需的ServletContext

我希望这个“可视化”的例子能有所帮助。

干杯

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48971937

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档