2013-10-11 73 views
2

在一個庫項目(使用Spring 3.2.4)中,我定義了多個攔截器。 servlet config xml文件包含在要導入到Web應用程序中的jar中。攔截器用於多個servlet xml中,因爲它們將用於具有不同攔截器的不同Dispatcher servlet。Spring HandlerInterceptor調用兩次

問題是,攔截器被調用兩次,但處理程序(控制器)只被調用一次。

的攔截器在庫項目定義:在JAR文件中被提供

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 { 
     } 
    } 

的servlet配置和後面包括在該應用程序。

libservletcfg.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <context:component-scan base-package="com.example.controller" annotation-config="true" /> 

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> 

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
     <property name="messageConverters"> 
      <list> 
       <ref bean="jacksonMessageConverter"/> 
      </list> 
     </property> 
    </bean> 

    <mvc:annotation-driven /> 

    <mvc:interceptors> 
     <mvc:interceptor> 
      <mvc:mapping path="/**"/> 
      <bean class="com.example.SomeInterceptor"/> 
     </mvc:interceptor>  
    </mvc:interceptors> 

</beans> 

在Web應用程序,我只是包括在servlet配置的libservletcfg.xml。

servletConfig.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 

    <import resource="classpath*:libservletcfg.xml"/> 

    <context:component-scan base-package="com.example.app.controller" annotation-config="true" /> 

</beans> 

這servletConfig.xml用作上下文配置的調度程序的servlet在web.xml:

<servlet> 
    <servlet-name>someServlet</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>classpath*:servletConfig.xml</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>someServlet</servlet-name> 
    <url-pattern>/someUrl/*</url-pattern> 
</servlet-mapping> 

誰能告訴我,爲什麼攔截器被調用兩次或任何配置問題(這可能會導致意外的行爲)?

編輯

一個叫兩聲控制器的實例:

@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; 
    } 

} 
+0

發佈攔截器被調用兩次的處理程序方法的示例。 –

+0

爲該問題添加了一個示例。 –

+0

嘗試調試並檢查您的'HandlerInterceptor'的兩個實例是否已註冊。 –

回答

1

檢查你沒有其他libservletcfg.xml遊逛。 classpath *:將匹配所有這些。

+0

情況並非如此。我搜索了IDE中的所有配置文件,甚至在提取的war文件中搜索了libservletcfg.xml文件,我只找到一個。 –