【问题标题】:SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializerSpringServletContainerInitializer 不能强制转换为 javax.servlet.ServletContainerInitializer
【发布时间】:2013-03-10 22:18:40
【问题描述】:

我正在尝试将基于 xml 的 Spring MVC 应用程序移动到基于 Java 配置的应用程序。似乎与 maven 中可用的各种 java.servlet 类不匹配。例如,有些提供 addServlet() 方法,有些不提供。

这是我的配置类:

public class MyWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext rootContext =
                new AnnotationConfigWebApplicationContext();
        rootContext.register(JpaSandboxConf.class);

        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet());
        registration.setLoadOnStartup(1);
        registration.addMapping("/myapp/*");
    }
}

这似乎没有争议。为了得到

container.addServlet()

方法,我在我的 pom 中包含了这个:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>7.0.30</version>
</dependency>

我试过了,但是下面条目中的 ServletContext 类不起作用(因为它没有提供 addServlet() 方法):

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>com.springsource.javax.servlet</artifactId>
    <version>2.5.0</version>
    <scope>provided</scope>
</dependency>

当我尝试使用

mvn clean tomcat7:run

我不开心

SEVERE: A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/]]
    at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:252)
    at java.util.concurrent.FutureTask.get(FutureTask.java:111)
    at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1130)
    at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:782)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1568)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1558)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
    at java.util.concurrent.FutureTask.run(FutureTask.java:166)
    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)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/]]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
    ... 7 more
Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer
    at org.apache.catalina.startup.ContextConfig.getServletContainerInitializer(ContextConfig.java:1543)
    at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1464)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1190)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:825)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:300)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5161)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 7 more

我看到一个类来自 Spring 框架,另一个来自 javax.servlet,但再一次,该方法不存在于 Spring 提供的类中(这令人沮丧,因为这个示例存在于 Spring 3.2 文档本身中.

我正在使用 Spring 3.2。我没有使用 Eclipse(所有示例都是基于 Eclipse,我在 IntelliJ 中)。这是一个maven项目

非常感谢您的帮助。

【问题讨论】:

标签: spring spring-mvc servlet-3.0


【解决方案1】:

这里有一个更好的 pom 中的 servlet 3.0 api 依赖项-https://github.com/SpringSource/greenhouse/tree/servlet3,您还应该将范围标记为provided,否则该 jar 将包含在您的最终战争中,这将干扰由容器,这就是您的情况似乎正在发生的事情。

【讨论】:

    【解决方案2】:

    我在没有 Spring 的情况下遇到了类似的问题,mvn tomcat7:run 不起作用,尽管 servlet 部署到 OpenShift 上很好。根据 Biju Kunjummen 的回答,对我来说问题是我正在编译哪个 servlet-api。

    破碎:

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <scope>provided</scope>
    </dependency>
    

    固定:

    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>servlet-api</artifactId>
        <version>3.0.20100224</version>
        <scope>provided</scope>
    </dependency>
    

    不太清楚为什么,但现在可以了。

    【讨论】:

      【解决方案3】:

      我用过 providedCompile 'javax.servlet:javax.servlet-api:3.1.0' 解决问题

      【讨论】:

        【解决方案4】:

        我在使用 gradle tomcat 插件时遇到了同样的问题。我将 tomcat 版本更新为 8 并且它工作正常。

        plugins {
            id "com.bmuschko.tomcat" version "2.2.2"
            id "java"
            id "eclipse"
            id "idea"
            id "war"
        }
        
        
        dependencies {
            providedCompile ('javax.servlet:javax.servlet-api:3.1.0')
            providedCompile ('javax.servlet.jsp:jsp-api:2.2')
            compile ('org.springframework:spring-webmvc:4.2.1.RELEASE')
        
            def tomcatVersion = '8.0.27'
            tomcat "org.apache.tomcat.embed:tomcat-embed-core:${tomcatVersion}",
                    "org.apache.tomcat.embed:tomcat-embed-logging-juli:${tomcatVersion}",
                    "org.apache.tomcat.embed:tomcat-embed-jasper:${tomcatVersion}"
        
        }
        

        【讨论】:

          【解决方案5】:

          在 Eclipse 中使用 Tomcat 时,我遇到了同样的错误。

          我通过转到“服务器”选项卡来解决这个问题 > 双击 Tomcat 并取消选中“服务模块而不发布”

          【讨论】:

            【解决方案6】:

            我也遇到了这个问题,我用他们的方法处理过,但是失败了。我使用的tomcat版本是7和8.0,我也遇到了这个问题。当我使用tomcat 8.5时,没有问题。我的项目运行成功。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-09-03
              • 2019-06-09
              • 2017-06-02
              • 2013-07-21
              • 2016-09-26
              • 1970-01-01
              • 2013-03-20
              相关资源
              最近更新 更多