1

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 :(

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

4
  • Don't extend WebMvcConfigurationSupport, either implement WebMvcConfigurere or if on older version extend WebMvcConfigurerAdapter. Commented Oct 3, 2019 at 8:56
  • Instead of WebApplicationInitializer, try extending AbstractAnnotationConfigDispatcherServletInitializer and override "getServletMappings", "getRootConfigClasses" and "getServletConfigClasses". Commented Oct 3, 2019 at 9:07
  • Same behaviour, cause org.springframework.web.context.support.ServletContextAwareProcessor can't set ServletContext (it's not existing at this time moment). This PostProcessor uses in both vaiarts Commented Oct 3, 2019 at 9:10
  • Too hard, isn't? Commented Oct 3, 2019 at 9:11

1 Answer 1

2

I found the solution: no need to call applicationContext.refresh(); in the org.springframework.web.WebApplicationInitializer#onStartup, cause it will be automaticaly called in the method org.springframework.web.servlet.FrameworkServlet#configureAndRefreshWebApplicationContext while servlet loading in the servlet container.

But I use example from spring.io docs: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet . Why they use it that way? proof

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.