关于过滤器Filter和拦截器Interceptor,你们都不会陌生,从一开始的servelet,到springmvc,再到如今的springboot,都有接触到,记得刚接触的时候,会容易弄混淆,想写这篇文章作个小的总结java
第一步:定义Filterspring
@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;
}
}
复制代码
第三步:定义拦截器api
@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容器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了bash
/** @deprecated */
@Deprecated
public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
public WebMvcConfigurerAdapter() {
}
}
复制代码
第五步:仍是启动以前的controllermvc
@RestController
@RequestMapping("/api/test")
@Slf4j
public class TestController {
@RequestMapping(value = "/hello")
public String test() {
log.info("test hello.............");
return "SUCCESS";
}
复制代码
看到打印结果以下app
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和我的博客:www.zplxjj.comide