Three interceptors in spring
Filter
Create a new TimeFilter
@ Component
Public class TimeFilter implements Filter {@ Overridebr/ > @ Override
System.out.println ("time filter init")
}
Overridepublic 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");} @ Overridepublic void destroy () {System.out.println ("time filter init");}
}
Start the server and enter: http://localhost:8080/hello?name=tom in the browser
You can output the following results in the console:
Time filter start
Name: tom
Time filter consume 3 ms
Time filter end
As you can see, filter executes first, and then actually executes the HelloController.sayHello () method.
As can be seen from the parameters of the TimeFilter.doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) method, we can only get the original request and response objects, not which Controller and which method the request was processed, and we can get this information using Interceptor.
Interceptor
Create a new TimeInterceptor
@ Component
Public class TimeInterceptor extends HandlerInterceptorAdapter {
Private final NamedThreadLocal 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 the handler information that processes the current request
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 into 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 {
/ / withdraw the startTime you 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 for TimeInterceptor
Inject TimeInterceptor into the spring container
@ Configuration
Public class WebConfig extends WebMvcConfigurerAdapter {
@ Autowiredprivate TimeInterceptor timeInterceptor;@Overridepublic void addInterceptors (InterceptorRegistry registry) {registry.addInterceptor (timeInterceptor);}
}
Start the server and enter: http://localhost:8080/hello?name=tom in the browser
You can output the following results 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, filter executes before interceptor, and then it actually executes the HelloController.sayHello () method. Through the handler parameter on the interceptor method, we can see which Controller and which method the request was processed by. However, the parameter value on this method cannot be obtained directly (in this case, the value of the parameter name of the HelloController.sayHello (String name) method), it can be obtained through Aspect.
Aspcet
Aspcet
Create a new TimeAspect
@ Aspect@Componentbr/ > @ 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;}
}
Start the server and enter: http://localhost:8080/hello?name=tom in the browser
You can output the following results 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 to interceptor, then to aspect, and then to the actual execution of the 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