【问题标题】:Failed to detect ServletContainerInitializers for context - Run time error in Spring MVC application无法检测上下文的 ServletContainerInitializers - Spring MVC 应用程序中的运行时错误
【发布时间】:2016-04-22 09:12:39
【问题描述】:

我在不使用原型的情况下从头开始创建 spring web-mvc 应用程序。 应用程序构建成功但创建但得到运行时异常 我正在使用 Tomcat-7,我在类路径中有 spring-webmvc。请检查下面的构建路径图像。需要帮助来理解这里的问题

严重:无法检测名称为 [/samplerestservice] 的上下文的 ServletContainerInitializers java.io.IOException:java.lang.ClassNotFoundException:org.springframework.web.SpringServletContainerInitializer 在 org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:199) 在 org.apache.catalina.startup.WebappServiceLoader.load(WebappServiceLoader.java:157) 在 org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1575) 在 org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1281) 在 org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889) 在 org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386) 在 org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) 在 org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) 在 org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5479) 在 org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 在 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574) 在 org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564) 在 java.util.concurrent.FutureTask.run(FutureTask.java:262) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 在 java.lang.Thread.run(Thread.java:745) 引起:java.lang.ClassNotFoundException:org.springframework.web.SpringServletContainerInitializer 在 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856) 在 org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705) 在 java.lang.Class.forName0(本机方法) 在 java.lang.Class.forName(Class.java:274) 在 org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:196) ... 15 更多

pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sample.restservice</groupId>
  <artifactId>samplerestservice</artifactId>
  <version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.1.9.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
         <exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>



</dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">

 <servlet>
    <servlet-name>restService-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>restService-dispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>

restService-dispatcher-servlet.xml

        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/conext
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">

       <context:component-scan base-package="samplerestservice" />
       <mvc:annotation-driven></mvc:annotation-driven>

</beans>

Build Path

Deployment Assembly

【问题讨论】:

标签: spring-mvc


【解决方案1】:

检查此错误行:

严重:无法检测 context with name [/samplerestservice]

的 ServletContainerInitializers

它期待您在代码中定义的一些 ContainerInitializerObject。

【讨论】:

  • 我没有清楚地理解答案。期待一些 ContainerInitializerObject 是什么意思?你是在建议我添加视图解析器吗?
【解决方案2】:

您有什么理由要手动配置所有这些东西?这些天开始使用 Spring 的推荐方法是使用 Spring Boot,它会根据 pom.xml 中的内容和类路径中的内容自动配置合理的默认值。如有必要,可以在 Java 和属性文件中对配置进行进一步的手动调整。与传统方式相比,它更清洁、更快、更省事。 spring-boot-web-starter 还嵌入了 tomcat 的实例,预配置为在端口 8080 上运行。spring 家伙提供了一个名为 SPRING INITIALIZR 的官方工具,您可以在其中选择要包含在 spring-boot 应用程序中的 spring 项目,它为您提供pom.xml 中包含所有正确内容和正确文件夹结构的项目。您只需使用 import -> existing maven project 将其导入 eclipse。您可以在几分钟内启动并运行,而不是几小时或几天。

【讨论】:

  • 我更喜欢不需要像自动配置这样的特殊需求的代码。我喜欢可以手动配置和可见的东西。它帮助我理解代码中的每一行
【解决方案3】:

从您在 Spring 问题跟踪器SPR-13872 上创建的问题来看,这个问题似乎与 Spring 没有严格关系,但可能来自其他问题。

【讨论】:

    【解决方案4】:

    问题与 Spring 框架 4.1.9.RELEASE 相关 更新到 4.0.5.RELEASE 后问题解决

    【讨论】:

    • 添加注释以及是否再次降级 jar 版本或进行了一些更改
    猜你喜欢
    • 2019-02-19
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-24
    • 1970-01-01
    • 2017-05-30
    • 2019-05-02
    相关资源
    最近更新 更多