Filter
新建 TimeFilter
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、網(wǎng)站制作、永新網(wǎng)絡(luò)推廣、小程序設(shè)計、永新網(wǎng)絡(luò)營銷、永新企業(yè)策劃、永新品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學(xué)生創(chuàng)業(yè)者提供永新建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:jinyejixie.com
@Component
public class TimeFilter implements Filter {@Override
br/>@Override
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"); }
}
啟動服務(wù)器,在瀏覽器輸入:http://localhost:8080/hello?name=tom
可以在控制臺輸出如下結(jié)果:
time filter start
name: tom
time filter consume 3 ms
time filter end
可以看到,filter 先執(zhí)行,再到真正執(zhí)行 HelloController.sayHello() 方法。
通過 TimeFilter.doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) 方法的參數(shù)可以看出,我們只能得到原始的 request 和 response對象,不能得到這個請求被哪個 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;
// 獲取處理當(dā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)前時間放入 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); }
}
啟動服務(wù)器,在瀏覽器輸入:http://localhost:8080/hello?name=tom
可以在控制臺輸出如下結(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() 方法。通過 interceptor 方法上的 handler 參數(shù),我們就可以得到這個請求被哪個 Controller 以及哪個方法處理了。但是不能直接獲取到這個方法上的參數(shù)值(在這里就是 HelloController.sayHello(String name) 方法參數(shù) name 的值),通過 Aspect 就可以獲取到。
Aspcet
Aspcet
新建 TimeAspect@Aspect@Component
br/>@Component@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; }
}
啟動服務(wù)器,在瀏覽器輸入:http://localhost:8080/hello?name=tom
可以在控制臺輸出如下結(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 的值。
請求攔截過程圖
graph TD
httprequest-->filter
filter-->interceptor
interceptor-->aspect
aspect-->controller
當(dāng)前文章:spring里的三大攔截器
網(wǎng)站地址:http://jinyejixie.com/article38/poeepp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、企業(yè)建站、電子商務(wù)、用戶體驗(yàn)、服務(wù)器托管、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)