title: Spring web : Failed to register servlet with name 'dispatcher'
body: <p>I start with Spring Web (following Spring documentation) and i have a little problem with the initialization</p>
  
  <p>Every time i have the stack trace : "java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name."</p>
  
  <p>Here are my files :</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee     http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
       version="3.1"&gt;
  
  &lt;session-config&gt;
      &lt;session-timeout&gt;
          30
      &lt;/session-timeout&gt;
  &lt;/session-config&gt;
  
  &lt;!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
  instead of the default XmlWebApplicationContext --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextClass&lt;/param-name&gt;
      &lt;param-value&gt;
          org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Configuration locations must consist of one or more comma- or space-delimited
  fully-qualified @Configuration classes. Fully-qualified packages may also be
  specified for component-scanning --&gt;
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;com.lacunasaurus.configuration.SpringApplicationConfiguration&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;!-- Bootstrap the root application context as usual using ContextLoaderListener --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Declare a Spring MVC DispatcherServlet as usual --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
      instead of the default XmlWebApplicationContext --&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextClass&lt;/param-name&gt;
          &lt;param-value&gt;
              org.springframework.web.context.support.AnnotationConfigWebApplicationContext
          &lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;!-- Again, config locations must consist of one or more comma- or space-delimited
      and fully-qualified @Configuration classes --&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;com.lacunasaurus.configuration.SpringWebApplicationInitializer&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/servlet&gt;
  </code></pre>
  
  <p></p>
  
  <p>home.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Start Page&lt;/title&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;Hello World!&lt;/h1&gt;
  
      Greeting : ${greeting}
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>ControllerHome</p>
  
  <pre><code>package com.lacunasaurus.web.controllers;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.ModelMap;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  @Controller
  @RequestMapping("/")
  public class ControllerHome {
  
      @RequestMapping(method = RequestMethod.GET)
      public String sayHello(ModelMap model) {
          model.addAttribute("greeting", "Hello World from Spring 4 MVC");
          return "welcome";
      }
  
      @RequestMapping(value = "/helloagain", method = RequestMethod.GET)
      public String sayHelloAgain(ModelMap model) {
          model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
          return "welcome";
      }
  
  }
  </code></pre>
  
  <p>Spring configuration </p>
  
  <pre><code>package com.lacunasaurus.configuration;
  
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  
  @Configuration
  @ComponentScan(basePackages = {"com.lacunasaurus.service"})
  public class SpringApplicationConfiguration {
  
  }
  </code></pre>
  
  <p>Webinitializer </p>
  
  <pre><code>package com.lacunasaurus.configuration;
  
  import     org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  
  public class SpringWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
  
  @Override
  protected Class&lt;?&gt;[] getRootConfigClasses() {
          return new Class[]{SpringApplicationConfiguration.class};
      }
  
      @Override
      protected Class&lt;?&gt;[] getServletConfigClasses() {
          return new Class[]{WebConfig.class};
      }
  
      @Override
      protected String[] getServletMappings() {
          return new String[]{"/*"};
      }
  }
  </code></pre>
  
  <p>And my webconfig</p>
  
  <pre><code>package com.lacunasaurus.configuration;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.ComponentScan;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.web.servlet.ViewResolver;
  import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  import org.springframework.web.servlet.view.InternalResourceViewResolver;
  
  @Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = "com.lacunasaurus.web.controllers")
  public class WebConfig {
  
      @Bean
      public ViewResolver viewResolver() {
          InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
          viewResolver.setPrefix("/WEB-INF/views/");
          viewResolver.setSuffix(".html");
  
          return viewResolver;
      }
  
  }
  </code></pre>
  
  <p>I use maven and "4.3.4.RELEASE" spring version</p>
  
  <p>My pom.xml</p>
  
  <pre><code>&lt;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/maven-v4_0_0.xsd"&gt;
  
  &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;com.lacunasaurus.vapologie&lt;/groupId&gt;
  &lt;artifactId&gt;test-web&lt;/artifactId&gt;
  &lt;packaging&gt;war&lt;/packaging&gt;
  &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
  &lt;name&gt;Vapologie Webapp&lt;/name&gt;
  
  &lt;properties&gt;
      &lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
      &lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
      &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
  &lt;/properties&gt;
  
  &lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
          &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
          &lt;version&gt;5.2.5.Final&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;5.1.6&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;4.3.4.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
          &lt;version&gt;4.3.4.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
          &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
          &lt;version&gt;3.1.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
          &lt;version&gt;4.3.4.RELEASE&lt;/version&gt;
      &lt;/dependency&gt;
  
  &lt;/dependencies&gt;
  
  &lt;build&gt;
      &lt;finalName&gt;test&lt;/finalName&gt;
      &lt;plugins&gt;
          &lt;plugin&gt;
              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
              &lt;version&gt;3.6.0&lt;/version&gt;
              &lt;configuration&gt;
                  &lt;showWarnings&gt;true&lt;/showWarnings&gt;
              &lt;/configuration&gt;
          &lt;/plugin&gt;
      &lt;/plugins&gt;
  &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>and the full stack trace :</p>
  
  <pre><code>12-Dec-2016 23:16:35.543 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
  org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/vapologie-web]]
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
  at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
  at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
  at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
  at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
  at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1780)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  Caused by: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name.
  at org.springframework.util.Assert.notNull(Assert.java:115)
  at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.registerDispatcherServlet(AbstractDispatcherServletInitializer.java:98)
  at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.onStartup(AbstractDispatcherServletInitializer.java:71)
  at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
  at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5170)
  at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
  ... 10 more
  
  12-Dec-2016 23:16:35.544 SEVERE [localhost-startStop-1]     org.apache.catalina.startup.HostConfig.deployDescriptor Erreur lors du déploiement du descripteur de configuration     C:\Users\Aurélien\AppData\Roaming\NetBeans\8.2\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\tes-web.xml
   java.lang.IllegalStateException: ContainerBase.addChild: start:     org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/vapologie-web]]
  at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
  at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
  at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
  at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
  at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1780)
  at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
  at java.util.concurrent.FutureTask.run(FutureTask.java:266)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
  at   java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
  at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>Thank for your help :)</p>