title: How exactly work the Spring HandlerInterceptor interface? Some doubts about a tutorial example
body: <p>I am studying on a tutorial the use of the <strong>interceptors</strong> in a Spring MVC application.</p>
  
  <p>So, from what I have undestand (correct me if I am doing wrong assertion), the JavaEE interceptors are used from cross cutting concern (AOP development). nterceptors are used in conjunction with Java EE managed classes to allow developers to invoke interceptor methods on an associated target class, in conjunction with method invocations or lifecycle events. Common uses of interceptors are logging, auditing, and profiling.</p>
  
  <p>So it seems to me that basically an interceptor can be used for task like: <strong>"When a specific method of a specific class is called do something before that this methos is executed</strong> or, at the contrary, <strong>"When a specific method of a specific class is called do something after that this methos is executed</strong>.</p>
  
  <p>Is it true or am I missing something?</p>
  
  <p>So in my example I have the following situation:</p>
  
  <p>1) Into the <strong>mvc-config.xml</strong> file (the file that configure the MVC) I can find:</p>
  
  <pre><code>&lt;mvc:interceptors&gt;
      &lt;mvc:interceptor&gt;
          &lt;mvc:mapping path="/*" /&gt;
          &lt;bean class="spring.mvc.interceptor.SiteInterceptor" /&gt;
      &lt;/mvc:interceptor&gt;
  
      &lt;mvc:interceptor&gt;
      &lt;mvc:mapping path="/*" /&gt;
          &lt;bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"&gt;
              &lt;property name="paramName" value="language" /&gt;
          &lt;/bean&gt;
      &lt;/mvc:interceptor&gt;
  &lt;/mvc:interceptors&gt;
  </code></pre>
  
  <p>that define 2 different interceptor, the first one is:</p>
  
  <pre><code>    &lt;mvc:interceptor&gt;
          &lt;mvc:mapping path="/*" /&gt;
          &lt;bean class="spring.mvc.interceptor.SiteInterceptor" /&gt;
      &lt;/mvc:interceptor&gt;
  </code></pre>
  
  <p>and it means that the <strong>SiteInterceptor</strong> is defined for all the http request (for all the resources).</p>
  
  <p>Then this is the <strong>SiteInterceptor</strong> code:</p>
  
  <pre><code>public class SiteInterceptor implements HandlerInterceptor, MessageSourceAware {
  
      private MessageSource messageSource;
  
      @Override
      public void setMessageSource(MessageSource messageSource) {
          this.messageSource = messageSource;     
      }
  
      @Override
      public boolean preHandle(HttpServletRequest request,
              HttpServletResponse response, Object handler) throws Exception {
          System.out.println("SiteInterceptor preHandle");
          return true;
      }
  
      @Override
      public void postHandle(HttpServletRequest request,
              HttpServletResponse response, Object handler,
              ModelAndView modelAndView) throws Exception {
              System.out.println("SiteInterceptor postHandle");   
  
  
      }
  
      @Override
      public void afterCompletion(HttpServletRequest request,
              HttpServletResponse response, Object handler, Exception exception)
              throws Exception {
          System.out.println("SiteInterceptor afterCompletion");  
          System.out.println("site.maintenance: " + messageSource.getMessage("site.maintenance", null, Locale.ENGLISH));
  
  
          System.out.println("site.maintenance: " + messageSource.getMessage("test.param", null, Locale.ENGLISH));
      }
  
  }
  </code></pre>
  
  <p>So there are defined some methods and for what I have understood:</p>
  
  <ul>
  <li><p><strong>preHandle()</strong>: Is performed before that the called request (for example a page) is handled (so what exaclty it means? That it is performed before the execution of the controller method that handle this resource? or what?)</p></li>
  <li><p><strong>postHandle()</strong>: When is it performed? When the controller method have completed its execution and before that the view is rendered? or what?</p></li>
  <li><p><strong>afterCompletion()</strong>: Is it performed when the view is rendered? (after that the Spring view resolver do its job?)</p></li>
  </ul>
  
  <p>Can you help me to clarify my doubts?</p>
  
  <p>Another doubts that I have are:</p>
  
  <p>1) So it seems to me that these method are used to do some specific cross cutting task before and after that a controller method have handled an HttpRequest (for example something like: "log when enter and when exit from a controller method). But can I do the same task using the <strong>Spring AOP</strong> module? What is the better solution? Why Spring provide me this solution if I can do the same task using <strong>Spring AOP</strong>?</p>
  
  <p>2) Exist some relation between the <strong>interceptor</strong> concept and the <strong>filter</strong> concept?</p>