您好,登錄后才能下訂單哦!
小編給大家分享一下java中spring里的三大攔截器分別是什么,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
Filter
新建 TimeFilter
@Component public class TimeFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("time filter init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("time filter start"); long startTime = System.currentTimeMillis(); filterChain.doFilter(servletRequest, servletResponse); long endTime = System.currentTimeMillis(); System.out.println("time filter consume " + (endTime - startTime) + " ms"); System.out.println("time filter end"); } @Override public void destroy() { System.out.println("time filter init"); } }
啟動(dòng)服務(wù)器,在瀏覽器輸入:http://localhost:8080/hello?name=tom
可以在控制臺(tái)輸出如下結(jié)果:
time filter start
name: tom
time filter consume 3 ms
time filter end
可以看到,filter 先執(zhí)行,再到真正執(zhí)行 HelloController.sayHello() 方法。
通過(guò) TimeFilter.doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) 方法的參數(shù)可以看出,我們只能得到原始的 request 和 response
對(duì)象,不能得到這個(gè)請(qǐng)求被哪個(gè) Controller 以及哪個(gè)方法處理了,使用Interceptor 就可以獲得這些信息。
Interceptor
新建 TimeInterceptor
@Component public class TimeInterceptor extends HandlerInterceptorAdapter { private final NamedThreadLocal<Long> startTimeThreadLocal = new NamedThreadLocal<>("startTimeThreadLocal"); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println("time interceptor preHandle"); HandlerMethod handlerMethod = (HandlerMethod) handler; // 獲取處理當(dāng)前請(qǐng)求的 handler 信息 System.out.println("handler 類:" + handlerMethod.getBeanType().getName()); System.out.println("handler 方法:" + handlerMethod.getMethod().getName()); MethodParameter[] methodParameters = handlerMethod.getMethodParameters(); for (MethodParameter methodParameter : methodParameters) { String parameterName = methodParameter.getParameterName(); // 只能獲取參數(shù)的名稱,不能獲取到參數(shù)的值 //System.out.println("parameterName: " + parameterName); } // 把當(dāng)前時(shí)間放入 threadLocal startTimeThreadLocal.set(System.currentTimeMillis()); return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("time interceptor postHandle"); } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { // 從 threadLocal 取出剛才存入的 startTime Long startTime = startTimeThreadLocal.get(); long endTime = System.currentTimeMillis(); System.out.println("time interceptor consume " + (endTime - startTime) + " ms"); System.out.println("time interceptor afterCompletion"); } }
注冊(cè) TimeInterceptor
把 TimeInterceptor 注入 spring 容器
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Autowired private TimeInterceptor timeInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(timeInterceptor); } }
啟動(dòng)服務(wù)器,在瀏覽器輸入:http://localhost:8080/hello?name=tom
可以在控制臺(tái)輸出如下結(jié)果:
time filter start time interceptor preHandle handler 類:com.nextyu.demo.web.controller.HelloController handler 方法:sayHello name: tom time interceptor postHandle time interceptor consume 40 ms time interceptor afterCompletion time filter consume 51 ms time filter end
可以看到,filter 先于 interceptor 執(zhí)行,再到真正執(zhí)行 HelloController.sayHello() 方法。通過(guò) interceptor 方法上的 handler 參數(shù),我們就可以得到這個(gè)請(qǐng)求被哪個(gè) Controller 以及哪個(gè)方法處理了
。但是不能直接獲取到這個(gè)方法上的參數(shù)值(在這里就是 HelloController.sayHello(String name) 方法參數(shù) name 的值),通過(guò) Aspect 就可以獲取到。
Aspcet
新建 TimeAspect
@Aspect @Component public class TimeAspect { @Around("execution(* com.nextyu.demo.web.controller.*.*(..))") public Object handleControllerMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("time aspect start"); Object[] args = pjp.getArgs(); for (Object arg : args) { System.out.println("arg is " + arg); } long startTime = System.currentTimeMillis(); Object object = pjp.proceed(); long endTime = System.currentTimeMillis(); System.out.println("time aspect consume " + (endTime - startTime) + " ms"); System.out.println("time aspect end"); return object; } }
啟動(dòng)服務(wù)器,在瀏覽器輸入:http://localhost:8080/hello?name=tom
可以在控制臺(tái)輸出如下結(jié)果:
time filter start time interceptor preHandle handler 類:com.nextyu.demo.web.controller.HelloController handler 方法:sayHello time aspect start arg is tom name: tom time aspect consume 0 ms time aspect end time interceptor postHandle time interceptor consume 2 ms time interceptor afterCompletion time filter consume 4 ms time filter end
可以看到,filter 先執(zhí)行,再到 interceptor 執(zhí)行,再到 aspect 執(zhí)行,再到真正執(zhí)行 HelloController.sayHello() 方法。
我們也獲取到了 HelloController.sayHello(String name) 方法參數(shù) name 的值
。
請(qǐng)求攔截過(guò)程圖
graph TD httprequest-->filter filter-->interceptor interceptor-->aspect aspect-->controller
以上是java中spring里的三大攔截器分別是什么的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。