Filter
New 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"); } }
Start the server and enter http://localhost:8080/hello?name=tom in the browser
The following results can be output in the console:
time filter start
name: tom
time filter consume 3 ms
time filter end
As you can see, the filter executes first, then the HelloController.sayHello() method is actually executed.
From the parameters of the TimeFilter. doFilter (ServletRequest servlet Request, ServletResponse servlet Response, FilterChain filter Chain) method, we can see that we can only get the original request and response object, and can not get which Controller and which method the request is processed by, so we can get the information using Interceptor.
Interceptor
New 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; // Get handler information for processing current requests System.out.println("handler Class:" + handlerMethod.getBeanType().getName()); System.out.println("handler Method:" + handlerMethod.getMethod().getName()); MethodParameter[] methodParameters = handlerMethod.getMethodParameters(); for (MethodParameter methodParameter : methodParameters) { String parameterName = methodParameter.getParameterName(); // You can only get the name of the parameter, not the value of the parameter. //System.out.println("parameterName: " + parameterName); } // Put the current time in 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 { // Remove the startTime just saved from threadLocal Long startTime = startTimeThreadLocal.get(); long endTime = System.currentTimeMillis(); System.out.println("time interceptor consume " + (endTime - startTime) + " ms"); System.out.println("time interceptor afterCompletion"); } }
Register Time Interceptor
Injecting Time Interceptor into spring Container
@Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Autowired private TimeInterceptor timeInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(timeInterceptor); } }
Start the server and enter http://localhost:8080/hello?name=tom in the browser
The following results can be output in the console:
time filter start time interceptor preHandle handler Class: com.nextyu.demo.web.controller.HelloController handler Method: sayHello name: tom time interceptor postHandle time interceptor consume 40 ms time interceptor afterCompletion time filter consume 51 ms time filter end
As you can see, the filter executes before the interceptor, and then the HelloController.sayHello() method is actually executed. With the handler parameter on the interceptor method, we can get which Controller the request is processed by and which method. But you can't get the parameter value on this method directly (in this case, the value of HelloController.sayHello(String name) method parameter name), and you can get it through Aspect.
Aspcet
New 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; } }
Start the server and enter http://localhost:8080/hello?name=tom in the browser
The following results can be output in the console:
time filter start time interceptor preHandle handler Class: com.nextyu.demo.web.controller.HelloController handler Method: 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
As you can see, filter executes first, then interceptor, then aspect, and then HelloController.sayHello() method.
We also get the value of the HelloController.sayHello(String name) method parameter name.
Request Interception Process Diagram
graph TD httprequest-->filter filter-->interceptor interceptor-->aspect aspect-->controller