How to write an interceptor by hand. Suppose I now need a timing interceptor, and I want to print to the console the amount of time it takes to invoke a service lock. What should I do?

There are three types of interception mechanisms:

1. FilterYou get the HTTP request, but no information about how to process it.
2. InterceptorCan get HTTP request information, also can get the request method information, but can not get the method parameter information.
3. Section (Aspect)Method parameter information is available, but HTTP request information is not available.

Each of them has its own strengths and weaknesses, and needs to choose the most appropriate interception mechanism based on its business needs.

Okay, let’s start the text.

Handwritten interceptor combat

*/ @component public class TimeInterceptor implements HandlerInterceptor * Created by fant.j. */ @component public class TimeInterceptor implements HandlerInterceptor @override public Boolean preHandle(HttpServletRequest HttpServletRequest, HttpServletResponse httpServletResponse, Object handler) throws Exception { System.out.println("preHandle");

        System.out.println(((HandlerMethod)handler).getBean().getClass().getName());
        System.out.println(((HandlerMethod)handler).getMethod().getName());
        httpServletRequest.setAttribute("startTime",System.currentTimeMillis());
        return true; } //controller is called after it is called, @override public void postHandle(HttpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception { System.out.println("postHandle");

        long startTime = (long) httpServletRequest.getAttribute("startTime");
        System.out.println("Time interceptor time :"+(System.currentTimeMillis() -startTime)); } //controller is called after it is called, @Override public void afterCompletion(HttpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception { System.out.println("afterCompletion");
        long startTime = (long) httpServletRequest.getAttribute("startTime");
        System.out.println("Time interceptor time :"+(System.currentTimeMillis() -startTime)); }}Copy the code

Code explanation:

  1. Where the preHandle() method is called before the controller is called
  2. PostHandle () is called after the controller call, not if there is an exception
  3. AfterCompletion () is called after the Controller is called, with or without exceptions, and the Exception parameter contains the Exception information.

However, it is not enough to write this interceptor. Further configuration is required, as shown in the following code:

/** * Created by fant.j. */ @configuration Public class WebConfig extends WebMvcConfigurerAdapter{// Inject Time interceptor @autoWired Private TimeInterceptor TimeInterceptor; @override public void addInterceptors(InterceptorRegistry registry) {// addInterceptors to the InterceptorRegistry registry.addInterceptor(timeInterceptor); }Copy the code

First we inherit the WebMvcConfigurerAdapter class and override its addInterceptors() method, which adds an interceptor to the Spring container. It then calls the InterceptorRegistry to register.

Here are all my essays:

Popular frameworks

SpringCloud springboot nginx redis

Underlying implementation principle:

Java NIO Tutorial Java Reflection Tutorial Java Concurrent Learning Notes Java Servlet Tutorial JDBC component tutorial Java NIO Tutorial Java language/version studies