burning 2019-07-01
关于过滤器Filter和拦截器Interceptor,大家都不会陌生,从一开始的servelet,到springmvc,再到现在的springboot,都有接触到,记得刚接触的时候,会容易弄混淆,想写这篇文章做个小的总结
相同点
不同点:
第一步:定义Filter
@Slf4j public class TestFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { log.info("TestFilter filter。。。。。。。。"); filterChain.doFilter(servletRequest, servletResponse); } }
第二步:注入springboot容器当中
@Configuration public class FilterConfig { @Bean Filter testFilter(){ return new TestFilter(); } @Bean public FilterRegistrationBean<TestFilter> filterRegistrationBean1(){ FilterRegistrationBean<TestFilter> filterRegistrationBean=new FilterRegistrationBean<>(); filterRegistrationBean.setFilter((TestFilter) testFilter()); filterRegistrationBean.addUrlPatterns("/*"); //filterRegistrationBean.setOrder();多个filter的时候order的数值越小 则优先级越高 return filterRegistrationBean; } }
第三步:定义拦截器
@Slf4j @Service(value = "testInterceptor") public class TestInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { log.info("TestInterceptor preHandle...."); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception { log.info("TestInterceptor postHandle...."); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception { log.info("TestInterceptor afterCompletion...."); } }
第四步:加入springboot容器
@Configuration public class InterceptorConfig implements WebMvcConfigurer { @Autowired TestInterceptor testInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(testInterceptor) .addPathPatterns("/api/**"); } }
注意:这边用的springboot是2.0.x,采取的是直接实现WebMvcConfigurer,因为WebMvcConfigurerAdapter被标识了@Deprecated,就没有继承WebMvcConfigurerAdapter了
/** @deprecated */ @Deprecated public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer { public WebMvcConfigurerAdapter() { } }
第五步:还是启动之前的controller
@RestController @RequestMapping("/api/test") @Slf4j public class TestController { @RequestMapping(value = "/hello") public String test() { log.info("test hello............."); return "SUCCESS"; }
看到打印结果如下
2019-04-27/12:01:04.603||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.filter.TestFilter 22 - TestFilter filter。。。。。。。。 2019-04-27/12:01:04.612||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.interceptor.TestInterceptor 26 - TestInterceptor preHandle.... 2019-04-27/12:01:04.634||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.interceptor.TestInterceptor 32 - TestInterceptor postHandle.... 2019-04-27/12:01:04.634||||||||^_^|[http-nio-8088-exec-1] INFO com.stone.zplxjj.interceptor.TestInterceptor 37 - TestInterceptor afterCompletion....
过滤器的实现基于回调函数。而拦截器(代理模式)的实现基于反射,代理又分静态代理和动态代理,动态代理是拦截器的简单实现。那何时使用拦截器?何时使用过滤器?
更多文章可以关注公众号:stonezplxjj和个人博客:http://www.zplxjj.com