【发布时间】:2013-10-11 10:34:43
【问题描述】:
在一个库项目中(使用 Spring 3.2.4)我定义了多个拦截器。 servlet 配置 xml 文件包含在要导入 Web 应用程序的 jar 中。拦截器用于多个 servlet xml,因为它们应用于具有不同拦截器的不同 Dispatcher servlet。
问题是,拦截器被调用了两次,但处理程序(控制器)只被调用了一次。
拦截器在库项目中定义:
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 配置以 jar 文件的形式提供,随后包含在应用程序中。
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 应用程序中,我只是将 libservletcfg.xml 包含在 servlet 配置中。
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 用作 web.xml 中 Dispatcher servlet 的上下文配置:
<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;
}
}
【问题讨论】:
-
贴一个处理方法的例子,拦截器被调用了两次。
-
在问题中添加了一个示例。
-
尝试调试并检查您的
HandlerInterceptor的两个实例是否已注册。 -
做到了。
HandlerInterceptor有两个不同的实例。到达断点的顺序:Instance1.preHandle(...), Instance2.preHandle(...), Handler, Instance2.postHandle(...), Instance1.postHandle(...)
标签: java spring spring-mvc