Java之Interceptor和Filter
?
最近研究了下Spring的HandlerInterceptor和Java的Filter,因为经常搞混它们两个,今天整理个笔记记录一下。
?
HandlerInterceptor 是Spring里面的拦截器
Filter是Java里面的过滤器
?
共同点 还是贴下Java里面的注释吧,解释还是很到位的:
?* <p>A HandlerInterceptor gets called before the appropriate HandlerAdapter
?* triggers the execution of the handler itself. This mechanism can be used
?* for a large field of preprocessing aspects, e.g. for authorization checks,
?* or common handler behavior like locale or theme changes. Its main purpose
?
?* is to allow for factoring out repetitive handler code.
?
?* <p>HandlerInterceptor is basically similar to a Servlet 2.3 Filter, but in
?* contrast to the latter it just allows custom pre-processing with the option
?* of prohibiting the execution of the handler itself, and custom post-processing.
?* Filters are more powerful, for example they allow for exchanging the request
?* and response objects that are handed down the chain. Note that a filter
?* gets configured in web.xml, a HandlerInterceptor in the application context.
?*
?* <p>As a basic guideline, fine-grained handler-related preprocessing tasks are
?* candidates for HandlerInterceptor implementations, especially factored-out
?* common handler code and authorization checks. On the other hand, a Filter
?* is well-suited for request content and view content handling, like multipart
?* forms and GZIP compression. This typically shows when one needs to map the
?* filter to certain content types (e.g. images), or to all requests.
?
? 解释下吧:
? HandlerInteceptor一般用于权限验证,以及一些处理风格本地化等公共代码。
? Filter一般用于修改请求内容和界面的解析处理相关。
?
? Inteceptor的三个方法:
??
//做实际的请求之前调用 //返回true会接着链式调用 //返回false终止链式调用 boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception; //请求之后,解析视图界面之前调用 void postHandle( HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception; //解析完界面之后调用 void afterCompletion( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception; // 在applicationContext.xml中配置 <!--interceptors begin--> <mvc:interceptors> <bean class="com.xxx.AllInterceptor"/> </mvc:interceptors> <!--interceptors end-->
? ?
? ? Filter配置
? ??
@Component public class RequestLogFilter implements Filter{ @Override public void destroy() { System.out.println("destory"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("doFilter"); chain.doFilter(request , response); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub System.out.println("init"); } } // web.xml配置 <filter> <filter-name>testFilter</filter-name> <filter-class>com.abcd.system.RequestLogFilter</filter-class> </filter> <filter-mapping> <filter-name>testFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
? ?
? ?测试结果顺序:发现filter会在前面处理
? ?init
? ?doFilter
? ?preHandle
? ?postHandle
? ?afterCompletion
原文:http://labreeze.iteye.com/blog/2213653