Satya
Satya

Reputation: 358

Failed to detect ServletContainerInitializers for context - Run time error in Spring MVC application

I am creating spring web-mvc application from scratch without using archetype. Application build successfully but creating but getting run-time exception I am using Tomcat-7, I have spring-webmvc in classpath. Please check buildpath image below. Need help to understand issue here

SEVERE: Failed to detect ServletContainerInitializers for context with name [/samplerestservice] java.io.IOException: java.lang.ClassNotFoundException: org.springframework.web.SpringServletContainerInitializer at org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:199) at org.apache.catalina.startup.WebappServiceLoader.load(WebappServiceLoader.java:157) at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1575) at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1281) at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889) at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5479) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1574) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1564) at java.util.concurrent.FutureTask.run(FutureTask.java:262) 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:745) Caused by: java.lang.ClassNotFoundException: org.springframework.web.SpringServletContainerInitializer at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856) at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:274) at org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:196) ... 15 more

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

Upvotes: 0

Views: 7755

Answers (4)

Brian Clozel
Brian Clozel

Reputation: 59221

Judging from the issue you created on the Spring issue tracker, SPR-13872, it seems this problem is not strictly related to Spring but may come from something else.

Upvotes: 0

Satya
Satya

Reputation: 358

issue was related to spring framework 4.1.9.RELEASE After updating to 4.0.5.RELEASE Issue resolved

Upvotes: -1

james_s_tayler
james_s_tayler

Reputation: 1943

Any reason you want to configure all this stuff by hand? The recommended way to get started with Spring these days is to use Spring Boot which auto-configures sensible defaults based on what's in your pom.xml and what's on the classpath. Further manual tweaks to configuration can be done in Java and properties files where necessary. It's cleaner, faster and way less hassle than the traditional way. The spring-boot-web-starter also embeds an instance of tomcat preconfigured to run on port 8080. The spring guys provide an official tool called the SPRING INITIALIZR where you can choose which spring projects to include in your spring-boot application and it gives you a project with all the right stuff in the pom.xml and the correct folder structure. You just need to import it into eclipse using import -> existing maven project. You can get up and running in minutes, not hours or days.

Upvotes: 0

Abhijit Kumar
Abhijit Kumar

Reputation: 311

Check this error line:

SEVERE: Failed to detect ServletContainerInitializers for context with name [/samplerestservice]

It is expecting some ContainerInitializerObject you define in the code.

Upvotes: 0

Related Questions