title: Spring MVC: difference between <context:component-scan> and <annotation-driven /> tags?
body: <p>Some days ago I began to study this Spring Hello World Tutorial: <a href="http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/" rel="noreferrer">http://viralpatel.net/blogs/spring-3-mvc-create-hello-world-application-spring-3-mvc/</a></p>
  
  <p>In this tutorial Spring DispatcherServlet is configured using the <em>spring-servlet.xml</em> file, this one: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
   &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p="http://www.springframework.org/schema/p"
      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/context 
      http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
  &lt;context:component-scan base-package="net.viralpatel.spring3.controller" /&gt;
  
  &lt;bean id="viewResolver"
      class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
      &lt;property name="viewClass"
          value="org.springframework.web.servlet.view.JstlView" /&gt;
      &lt;property name="prefix" value="/WEB-INF/jsp/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>
  
  <p>In this file I am using the <strong>context:component-scan</strong> tag to say that Spring have to scan my file searching the annotation, so for example, when the controller class finds that a method is annotated by <strong>@RequestMapping("/hello")</strong> annotation knows that this method handles the HTTP Request toward the URL ending with "/hello". This is simple...</p>
  
  <p>Now my doubt is related to the Spring MVC template project that I could automatically build in STS\Eclipse. </p>
  
  <p>When I create a new Spring MVC project in STS I have that my <em>DispatcherServlet</em> is configured by a file named <strong>servlet-context.xml</strong> that contains some configuration similar to the previous example file.</p>
  
  <p>In this file, I still have  the component scan tag:</p>
  
  <pre><code>&lt;context:component-scan base-package="com.mycompany.maventestwebapp" /&gt;
  </code></pre>
  
  <p>but I have also another tag (that look like have similar task), this one:</p>
  
  <pre><code>&lt;annotation-driven /&gt;
  </code></pre>
  
  <p>What is the difference between these two tags?<br>
  An other "strange" thing is that the previous example (that don't use the annotation-driven tag) is very similar to the project create by STS using the Spring MVC Template project but if I delete the annotation-driven tag from its configuration file the project don't run and give me the following error: <strong>HTTP Status 404 -</strong></p>
  
  <p>And in the stacktrace I have:</p>
  
  <p><strong>WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/maventestwebapp/] in DispatcherServlet with name 'appServlet'</strong></p>
  
  <p>But why? The previous example works well without annotation-driven tag, and this controller class is very similar. In fact, there is only one method that handles HTTP request toward "/" path</p>
  
  <p>This is the code of my controller class:</p>
  
  <pre><code>package com.mycompany.maventestwebapp;
  
  import java.text.DateFormat;
  import java.util.Date;
  import java.util.Locale;
  
  import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.stereotype.Controller;
  import org.springframework.ui.Model;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  
  /**
   * Handles requests for the application home page.
  */
  @Controller
  public class HomeController {
  
  private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  
  /**
   * Simply selects the home view to render by returning its name.
   */
  @RequestMapping(value = "/", method = RequestMethod.GET)
  public String home(Locale locale, Model model) {
      logger.info("Welcome home! The client locale is {}.", locale);
  
      Date date = new Date();
      DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
  
      String formattedDate = dateFormat.format(date);
  
      model.addAttribute("serverTime", formattedDate );
  
      return "home";
  }
  </code></pre>
  
  <p>Can someone help me to understand this thing?</p>
  
  <p>Thank you very much!</p>