title: Spring HandlerInterceptor called twice
body: <p>In a library project (using Spring 3.2.4) I define multiple interceptors. The servlet config xml files are included in the jar to be imported in the web application. The interceptors are used in multiple servlet xmls, because they shall be used for different Dispatcher servlets with different interceptors.</p>
  
  <p>The problem is, that the Interceptors are called twice, but the handler(controller) is just called once.</p>
  
  <p>The interceptor is defined in the library project:</p>
  
  <pre><code>public class SomeInterceptor extends HandlerInterceptorAdapter {
  
          @Override
          public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception {
              System.out.println("afterCompletionCalled");
          }
  
          @Override
          public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
              System.out.println("preHandle called");
              return true;
          }
  
          @Override
          public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception {
          }
      }
  </code></pre>
  
  <p>The servlet configuration is provided in a jar file and later on included in the application.</p>
  
  <p>libservletcfg.xml:</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:context="http://www.springframework.org/schema/context"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;
  
      &lt;context:component-scan base-package="com.example.controller" annotation-config="true" /&gt;
  
      &lt;bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt;
          &lt;property name="messageConverters"&gt;
              &lt;list&gt;
                  &lt;ref bean="jacksonMessageConverter"/&gt;
              &lt;/list&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
     &lt;mvc:interceptors&gt;
          &lt;mvc:interceptor&gt;
              &lt;mvc:mapping path="/**"/&gt;
              &lt;bean class="com.example.SomeInterceptor"/&gt;
          &lt;/mvc:interceptor&gt;    
      &lt;/mvc:interceptors&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>In the web application I just include the libservletcfg.xml in the servlet config.</p>
  
  <p>servletConfig.xml:</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:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
      &lt;import resource="classpath*:libservletcfg.xml"/&gt;
  
      &lt;context:component-scan base-package="com.example.app.controller" annotation-config="true" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>This servletConfig.xml is used as context config for the Dispatcher servlet in the web.xml:</p>
  
  <pre><code>&lt;servlet&gt;
      &lt;servlet-name&gt;someServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;classpath*:servletConfig.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;someServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/someUrl/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>Can anyone tell my why the interceptor is called twice or any problems with the configuration (which might lead to an unexpected behaviour)?</p>
  
  <p><em>edit</em></p>
  
  <p>Example of a Controller called twice:</p>
  
  <pre><code>@Controller
  @RequestMapping(value = "/someUrl")
  public class SampleController {
  
      @RequestMapping(method = RequestMethod.POST)
      public @ResponseBody SampleResponseBody sampleMethod(@RequestBody final SampleRequestBody pSampleRequestBody) {
          final SampleResponseBody response = new SampleResponseBody();
          return response;
      }
  
  }
  </code></pre>