Spring AbstractAnnotationConfigDispatcherServletInitializer No WebApplicationContext found: no ContextLoaderListener registered

StackOverflow https://stackoverflow.com/questions/20509348

Question

I am using Spring 3.2.5 with no XML configuration. I am using the AbstractAnnotationConfigDispatcherServletInitializer to initialize my application and AbstractSecurityWebApplicationInitializer to initialize spring security.

My application deploys and runs file with I use the VMware vFabric tc Server Developer Edition v2.9 that ships with Spring Tool Suite. However, when I deploy to a standard Tomcat 7.0.12 using jdk 1.7.0_21 as the JR

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.

Apache Tomcat/7.0.12

The stacktrace is:

SEVERE: Servlet.service() for servlet [default] in context with path [/auctionmanagerMVC] threw exception
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:251)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:722)

Here is my AbstractAnnotationConfigDispatcherServletInitializer code:

public class CrmWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

@Override
protected Class<?>[] getRootConfigClasses() {

    return new Class<?>[]{ServiceConfiguration.class,SecurityConfig.class,SocialContext.class};
}

@Override
protected Class<?>[] getServletConfigClasses() {

    return new Class<?>[]{RestMvcConfiguration.class, WebMvcConfiguration.class};
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/"};
}

@Override
protected Filter[] getServletFilters() {
    return new Filter[]{new HiddenHttpMethodFilter(), new MultipartFilter(), new OpenEntityManagerInViewFilter()};
}
@Override
protected void registerDispatcherServlet(ServletContext servletContext) {
    super.registerDispatcherServlet(servletContext);

    servletContext.addListener(new HttpSessionEventPublisher());

}
@Override
protected WebApplicationContext createRootApplicationContext() {
    AnnotationConfigWebApplicationContext appContext = (AnnotationConfigWebApplicationContext)super.createRootApplicationContext();

    String profile;
     profile = "brett";

     appContext.getEnvironment().setActiveProfiles(profile);
     return appContext;
}

}
@Configuration
@ComponentScan(basePackages={"com.auctionmanagermvc","com.hodeltech"})
@EnableWebMvc
@EnableHypermediaSupport
class RestMvcConfiguration extends RepositoryRestMvcConfiguration {
}

Any help you can give me as to how to get this application to start in Tomcat would be much appreciated. I am not having luck deploying to cloud vendors such as Heroku or Amazon Elastic Beanstalk due to this issue.

Thank you very much in advance!

EDIT: Adding AbstractSecurityWebApplicationInitializer

public class CrmSecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
    /**
     * Instruct Spring Security to use the {@link DispatcherServlet}'s
     * {@link WebApplicationContext} to find the springSecurityFilterChain.
     */
    @Override
    protected String getDispatcherWebApplicationContextSuffix() {
        return AbstractDispatcherServletInitializer.DEFAULT_SERVLET_NAME;
    }

    /**
     * Insert the following filters before Spring Security. Be careful when inserting
     * filters before Spring Security!
     */
    @Override
    protected void afterSpringSecurityFilterChain(ServletContext servletContext) {
    //      insertFilters(servletContext, new HiddenHttpMethodFilter(), new MultipartFilter() , new OpenEntityManagerInViewFilter());
    }

    /**
     * Register the {@link HttpSessionEventPublisher}
     */
    @Override
    protected boolean enableHttpSessionEventPublisher() {
        return true;
    }

}

EDIT: One additional thing I noticed: When starting up using the vFabric server, it appeared as though spring scanned through all the classes twice. For example, I would put a logging statement in a configuration method and it would get output twice. When running under Tomcat, I am only seeing the logging statement once. I don't know if that has anything to do with this problem, but is an interesting difference I noticed.

Was it helpful?

Solution 3

So after more trial and error, I finally discovered what was causing the difference between Tomcat and VMware vFabric tc Server Developer Edition v2.9. vFabric is using Tomcat 7.0.47, and the Tomcat version I was using was 7.0.22. I also tried 7.0.40 running on Amazon Elastic Beanstalk, which also had a problem. But, upgrading to Tomcat 7.0.47 everything worked fine.

Thanks to all who looked into this issue.

OTHER TIPS

I was getting the same error and in my case also the solution was different. I hadn't overriden the getDispatcherWebApplicationContextSuffix method of AbstractSecurityWebApplicationInitializer.

After adding the implementation from your question, all started working:

@Override
protected String getDispatcherWebApplicationContextSuffix() {
    return AbstractDispatcherServletInitializer.DEFAULT_SERVLET_NAME;
}

It looks like you resolved the issue, however, I have experienced the same and the solution was slightly different. The solution which worked for me was to add WebConfig.class (my configuration class containing the @EnableWebMvc annotation) to the getRootConfigClasses() method. This enables Spring Security to get hold of the WebApplicationContext.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top