Spring AbstractAnnotationConfigDispatcherServletInitializer未找到WebApplicationContext:没有注册ContextLoaderListener

时间:2013-12-11 02:20:04

标签: java spring tomcat7 startup spring-java-config

我使用的是没有XML配置的Spring 3.2.5。我正在使用AbstractAnnotationConfigDispatcherServletInitializer初始化我的应用程序和AbstractSecurityWebApplicationInitializer来初始化spring安全性。

我的应用程序部署并运行文件,我使用Spring Tool Suite附带的VMware vFabric tc Server Developer Edition v2.9。但是,当我使用jdk 1.7.0_21作为JR

部署到标准Tomcat 7.0.12时
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

堆栈跟踪是:

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)

这是我的AbstractAnnotationConfigDispatcherServletInitializer代码:

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 {
}

您可以向我提供有关如何在Tomcat中启动此应用程序的任何帮助,我们将不胜感激。由于这个问题,我没有运气部署到Heroku或Amazon Elastic Beanstalk等云供应商。

非常感谢你!

编辑: 添加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;
    }

}

编辑:我注意到的另一件事: 当使用vFabric服务器启动时,看起来好像spring扫描了所有类两次。例如,我会在配置方法中放置一个日志语句,它会输出两次。在Tomcat下运行时,我只看到一次日志记录语句。我不知道这与这个问题有什么关系,但我注意到这是一个有趣的区别。

3 个答案:

答案 0 :(得分:3)

我得到了同样的错误,在我的情况下,解决方案也有所不同。 我没有覆盖AbstractSecurityWebApplicationInitializer的 getDispatcherWebApplicationContextSuffix 方法。

从您的问题中添加实现后,所有内容都开始工作:

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

答案 1 :(得分:1)

看起来你已经解决了这个问题,但是,我遇到了同样的问题,解决方案略有不同。对我有用的解决方案是将WebConfig.class(我的包含@EnableWebMvc注释的配置类)添加到getRootConfigClasses()方法。这使Spring Security能够获得WebApplicationContext

答案 2 :(得分:0)

因此,经过更多的反复试验,我终于发现了导致Tomcat和VMware vFabric tc Server Developer Edition v2.9之间差异的原因。 vFabric正在使用Tomcat 7.0.47,而我使用的Tomcat版本是7.0.22。我也尝试在Amazon Elastic Beanstalk上运行7.0.40,这也有问题。但是,升级到Tomcat 7.0.47一切正常。

感谢所有研究过这个问题的人。

相关问题