source

ugrade 스프링 부트 2.0.0.0.RC2 예외 서블릿 컨텍스트 집합 없음

lovecheck 2023. 7. 31. 21:28
반응형

ugrade 스프링 부트 2.0.0.0.RC2 예외 서블릿 컨텍스트 집합 없음

디버깅 후 문제는 mvc 구성 클래스 EnableWebMvcConfiguration이 너무 일찍 로드되고 서블릿이 아직 로드되지 않았다는 것입니다.

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

"디버깅 후 문제는 mvc 구성 클래스 EnableWebMvcConfiguration이 너무 일찍 로드되고 서블릿이 아직 로드되지 않았다는 것입니다."

저는 이것에 몇 시간을 소비했습니다.저는 왜 이런 일이 일어나는지 이유를 찾을 수 있었습니다.구성이 여러 파일로 분할되어 보안 구성(이전에 생성됨)에 MVC 관련 빈을 만들고 있었는데, 이 빈을 생성하기 전에 MVC 구성을 사용하도록 강제했습니다.

솔루션은 @Bean 인스턴스를 보안 구성에서 MVC 구성으로 이동하는 것이었습니다.저는 그것이 다른 사람들에게 도움이 되기를 바랍니다!

저도 "No ServletContext set" 문제가 있는데 간단한 데모를 통해 이해하고자 합니다.

주의:
제가 사용하는 예제는 OP와 약간 다르지만 컨셉과 이슈는 동일합니다.
관련 없는 로그를 일부 제거했습니다.

데모: 간단한 스프링 부트 애플리케이션:
본류BeanLoadingDemoApplication로 주석을 단.@SpringBootApplication,
a의class WebSecurityConfiguration extends WebSecurityConfigurerAdapter그것보다 우선하는 것.configure방법
테스트 콩 몇 개, 독립형으로 1개@Configuration수업, 그리고 주요한 다른 것.@SpringBootApplication학급

추신1WebApplicationContext개체는 웹 응용 프로그램의 서블릿 컨텍스트에 저장된 비즈니스 빈 정의를 기반으로 하는 스프링 컨테이너입니다.
추신2 그래서 초기화된 후에 여러분은 콩이 초기화되는 것을 보게 될 것입니다. (제 예에서는 콩을 테스트하고 콩을 테스트합니다.)

응용 프로그램을 시작한 후의 로그는 다음과 같습니다.이 부분이 로그의 어디에 있는지에만 초점을 맞춥니다.

INFO  o.s.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 700 ms
INFO  c.example.demo.WebSecurityConfiguration - WebSecurityConfiguration() - Constructor
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)


So far ok? Simple, right? Now i add a
@Bean
public ServletWebServerFactory servletContainer() { 
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
...
}

수업 중에WebSecurityConfigurationTomcat용 두 번째 커넥터를 구성합니다.이 부품이 "이동"한 위치에 초점을 맞춥니다.

웹 보안 구성() - 생성자

생성자가 응용 프로그램을 시작한 후 바로 호출되고 루트보다 먼저 생성된 것을 볼 수 있습니까?WebApplicationContext초기화되었습니까?지금쯤이면 문제는 분명합니다.

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경유로@Bean Tomcat을 초기화하기 전과 Root WebApplicationContext를 초기화하기 전!!하지만 나의@Bean안에 있습니다.WebSecurityConfiguration class해당 클래스는 원래보다 훨씬 일찍 구축되고 있으며 결과적으로 서블릿 컨텍스트가 없습니다!!

해결책은 이것을 옮기는 것입니다.

@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    ...
}

순식간에@Configuration 클래스,즉 합니다.WebSecurityConfiguration원래보다 일찍 호출되지 않으며, 그렇게 함으로써, 그것은 다음을 얻을 것입니다.ServletContext필요합니다.

저는 이 "시각적" 사례가 도움이 되기를 바랍니다.

건배.

언급URL : https://stackoverflow.com/questions/48971937/ugrade-spring-boot-2-0-0-rc2-exception-no-servletcontext-set

반응형