關於過濾器Filter和攔截器Interceptor,你們都不會陌生,從一開始的servelet,到springmvc,再到如今的springboot,都有接觸到,記得剛接觸的時候,會容易弄混淆,想寫這篇文章作個小的總結java
相同點spring
不一樣點:編程
第一步:定義Filterapi
@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容器當中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; } }
第三步:定義攔截器mvc
@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容器app
@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() { } }
第五步:仍是啓動以前的controlleride
@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