spring SpringServletContainerInitializer 不能转换为 javax.servlet.ServletContainerInitializer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15328363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:48:36  来源:igfitidea点击:

SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer

springspring-mvcservlet-3.0

提问by Brian Dolan

I am attempting to move a xml-based Spring MVC app to a Java Configuration based app. There appears to be a mismatch with the various java.servlet classes available in maven. For instance, some provide the addServlet() method and some do not.

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

Here is my config class:

这是我的配置类:

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/*");
    }
}

Which seems rather uncontroversial. In order to get the

这似乎相当没有争议。为了获得

container.addServlet()

method, I included this in my pom:

方法,我将其包含在我的 pom 中:

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

I tried this, but the ServletContext class from the entry below DID NOT WORK (in that it didnot provide the addServlet() method):

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

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

When I attempt to run this using

当我尝试使用

mvn clean tomcat7:run

I unhappily obtain

我不幸地获得

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

I see that one class is coming from Spring framework and the other from javax.servlet, but once again, the method does not exist from the Spring provided class (which is frustrating, because this example exists within the Spring 3.2 documentation itself.

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

I am using Spring 3.2. I am NOT using Eclipse (all the examples are Eclipse based, I'm in IntelliJ). This is a maven project

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

I DO appreciate your help.

我非常感谢你的帮助。

回答by Biju Kunjummen

A better servlet 3.0 api dependency entry in pom is available here - https://github.com/SpringSource/greenhouse/tree/servlet3, you should also mark the scope as provided, otherwise the jar will be included in your final war which will interfere with the jar provided by the container which is what seems to be happening in your case.

此处提供了 pom 中更好的 servlet 3.0 api 依赖项 - https://github.com/SpringSource/greenhouse/tree/servlet3,您还应该将范围标记为provided,否则该 jar 将包含在您的最终War中,这会造成干扰使用容器提供的罐子,这似乎是您的情况。

回答by KevinL

I had a similar problem without Spring, where mvn tomcat7:rundid not work, despite the servlet deploying to OpenShift just fine. As per Biju Kunjummen's answer, the problem for me was which servlet-api I was compiling against.

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

Broken:

破碎的:

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

Fixed:

固定的:

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

Not terribly sure why, but it works now.

不太确定为什么,但它现在有效。

回答by abhi shukla

I was facing the same problem wis gradle tomcat plugin. I updated the tomcat version to 8 and it worked.

我在 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}"

}

回答by Saurabh

I used providedCompile 'javax.servlet:javax.servlet-api:3.1.0'to solve the issue

我曾经 providedCompile 'javax.servlet:javax.servlet-api:3.1.0'解决过这个问题

回答by Mrye

I am facing the same error when using Tomcat in Eclipse.

在 Eclipse 中使用 Tomcat 时,我面临同样的错误。

I solve this by going to Servers tab > double click the Tomcat and uncheck the "Serve modules without publishing"

我通过转到“服务器”选项卡 > 双击 Tomcat 并取消选中“无需发布即可提供模块”来解决此问题

回答by varuscn

I alse get this problem, I use their method to handle it, but failed. my tomcat version i used are 7 and 8.0 , i got get this problem too. when i use tomcat 8.5, there is no problem. my project run successfully.

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