這篇文章帶給大家的內容是介紹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"); } }
啟動伺服器,在瀏覽器輸入:http://localhost:8080/ hello?name=tom
可以在控制台輸出如下結果:
time filter start
name: tom
time filter consume 3 ms
time filter end
可以看到,filter 先執行,再到真正執行HelloController.sayHello() 方法。
透過TimeFilter.doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) 方法的參數可以看出,我們只能得到原始的request 和response
哪個對象,不能得到這個請求被哪個Controller 以及哪個Controller 以及哪個方法處理了,使用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; // 获取处理当前请求的 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(); // 只能获取参数的名称,不能获取到参数的值 //System.out.println("parameterName: " + parameterName); } // 把当前时间放入 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"); } }
註冊TimeInterceptor
把TimeInterceptor 注入spring 容器
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Autowired private TimeInterceptor timeInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(timeInterceptor); } }
啟動伺服器,在瀏覽器輸入:http://localhost:8080/hello?name=tom
可以在控制台輸出如下結果:
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 執行,再到真正執行HelloController.sayHello() 方法。透過 interceptor 方法上的 handler 參數,我們就可以得到這個請求被哪個 Controller 以及哪個方法處理了
。但不能直接取得到這個方法上的參數值(這裡就是 HelloController.sayHello(String name) 方法參數 name 的值),透過 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; } }
啟動伺服器,在瀏覽器輸入:http://localhost:8080/ hello?name=tom
可以在控制台輸出如下結果:
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 先執行,再到interceptor 執行,再到aspect 執行,再到真正執行HelloController.sayHello()方法。
我們也取得了 HelloController.sayHello(String name) 方法參數 name 的值
。
請求攔截過程圖
graph TD httprequest-->filter filter-->interceptor interceptor-->aspect aspect-->controller
以上是java學習之spring裡的三大攔截器是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!