I'm triyng to deploy a spring-mvc webapp WAR-package on tomcat. Deploy proccess fails with following error: 'java.lang.IllegalStateException: No ServletContext set'
I guess something wrong with my configuration :(
- Tomcat version: 8.5.46
- Spring, spring-mvc version: 5.1.9.RELEASE
- Example configuration code from spring.io: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet
My webapp initializer:
package com.jbtits.spring.mvc.webac;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(AppConfig.class);
applicationContext.refresh();
DispatcherServlet servlet = new DispatcherServlet(applicationContext);
ServletRegistration.Dynamic registration = servletContext.addServlet("webac", servlet);
registration.setLoadOnStartup(1);
registration.addMapping("/");
}
}
My webapp configuration:
package com.jbtits.spring.mvc.webac;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
@EnableWebMvc
@ComponentScan("com.jbtits.spring.mvc.webac")
public class AppConfig extends WebMvcConfigurationSupport {
}
Thats it, only two beans.
Tomcat failure output:
02-Oct-2019 18:02:52.971 WARNING [http-nio-8081-exec-84] org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration: 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
WebMvcConfigurationSupport, either implementWebMvcConfigurereor if on older version extendWebMvcConfigurerAdapter.org.springframework.web.context.support.ServletContextAwareProcessorcan't setServletContext(it's not existing at this time moment). ThisPostProcessoruses in both vaiarts