春季3拦截器顺序 [英] Spring 3 Interceptor Order
问题描述
我有一个实现两个拦截器的Spring 3 Web App.我正在使用带有@Configuration注释的配置类.代码如下:
I have a Spring 3 Web App that implements two interceptors. Im using a config class annotated @Configuration. The code is as follows:
@Override
public void addInterceptors(InterceptorRegistry registry) {
// TODO Auto-generated method stub
super.addInterceptors(registry);
registry.addInterceptor(homeInterceptor()).addPathPatterns("/");
registry.addInterceptor(allInterceptor());
}
无论我将拦截器添加到注册表的顺序如何,始终在homeInterceptor的preHandle之前调用allInterceptor的preHandle函数.有谁知道如何控制拦截器的调用顺序?
No matter what order I add the interceptors to the registry, the allInterceptor's preHandle function is always called before the homeInterceptor's preHandle. Does anyone know how to control the order that interceptors are invoked?
谢谢!
推荐答案
我查看了基础实现,全局拦截器(不与任何路径映射关联)在映射的拦截器(具有关联的路径模式)之前执行.因此,如果希望homeInterceptor
在allInterceptor
之前执行,则可能必须将allInterceptor
设置为映射的拦截器(通过提供路径模式).
I looked at the underlying implementation, the global interceptors(not associated to any path mapping) get executed before the mapped interceptors (with associated path patterns). So if you want the homeInterceptor
to be executed before the allInterceptor
, the allInterceptor
may have to be made a mapped interceptor(by providing a path pattern).
这是记录拦截器并在运行时查找拦截器的两种方法:
org.springframework.web.servlet.handler.AbstractHandlerMapping.initInterceptors()
These are the two methods that record the interceptors and find the interceptors at runtime:
org.springframework.web.servlet.handler.AbstractHandlerMapping.initInterceptors()
org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandlerExecutionChain(Object, HttpServletRequest)
这篇关于春季3拦截器顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!