在库项目中(使用Spring 3.2.4),我定义了多个拦截器。 servlet config xml文件包含在要在Web应用程序中导入的jar中。拦截器用于多个servlet xmls,因为它们应该用于具有不同拦截器的不同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应用程序中,我只是在servlet config中包含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用作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的示例:
@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 :(得分:2)
检查您是否还有其他 libservletcfg.xml 。 classpath *:将匹配所有这些。
答案 1 :(得分:1)
我们面临两次被拦截器调用的相同问题,这是因为我们在web.xml和@Configuration java文件中都定义了applicationContext.xml。
答案 2 :(得分:0)
您需要使用org.springframework.web.servlet.AsyncHandlerInterceptor:
public interface AsyncHandlerInterceptor extends HandlerInterceptor {
void afterConcurrentHandlingStarted(
HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception;
}
Spring MVC执行序列:
preHandle
afterConcurrentHandlingStarted
preHandle
postHandle
afterCompletion
从link找到并验证了自己
希望这会有所帮助!
答案 3 :(得分:0)
我也面临着同样的问题,拦截器被叫了两次。 问题出在根配置类的@ComponentScan声明中,正在创建两个实例,一个在Web上下文中,另一个在根上下文中。
因此,从根上下文中删除了拦截器包后,问题得到了解决。