In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Spring's interceptor to monitor the execution time of each Controller or method". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Spring's interceptor to monitor the execution time of each Controller or method".
Catalogue
The Spring interceptor monitors the execution time of each Controller or method
First write a class (TestInterceptor)
Next is the spring configuration file
The timing of execution of the three methods of interceptor
The three methods of interceptor are
The Spring interceptor monitors the execution time of each Controller or method. First write a class (TestInterceptor).
Let him inherit HandlerInterceptorAdapter and override three of these methods, such as:
Package com.wechat.test;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;import java.util.Calendar;import java.util.Date;/** * Created by lenovo on 2017-3-14. * / public class TestInterceptor extends HandlerInterceptorAdapter {private long time; private int openingTime; private int closingTime; private String mappingURL / / use regular mapping to the path public void setOpeningTime (int openingTime) {this.openingTime = openingTime;} public void setClosingTime (int closingTime) {this.closingTime = closingTime;} public void setMappingURL (String mappingURL) {this.mappingURL = mappingURL;} / * 1. Execute * @ param request * @ param response * @ param handler * @ return before entering the Controller method. If the return value is true, you can continue to execute Controller,preHandle () and afterCompletion (), and stop executing any method if it is false. * @ throws Exception * / @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {time = System.currentTimeMillis (); String url=request.getRequestURL () .toString (); if (mappingURL==null | | url.matches (mappingURL)) {Calendar c=Calendar.getInstance (); c.setTime (new Date ()); int now=c.get (Calendar.HOUR_OF_DAY) If (nowclosingTime) {request.setAttribute ("msg", "Registration opening hours:" + openingTime + ": 00 -" + closingTime + ": 00"); / / request.getRequestDispatcher ("/ msg.jsp") .forward (request, response); return false;} return true;} return true Execute * @ param request * @ param response * @ param handler * @ param modelAndView * @ throws Exception * / @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {super.postHandle (request, response, handler, modelAndView);} / * 3. View rendering completed (the whole request ends) execute * @ param request * @ param response * @ param handler * @ param ex * @ throws Exception * / @ Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println ("request processing is complete! Time: "+ (System.currentTimeMillis ()-time) +" ms "); super.afterCompletion (request, response, handler, ex);}} next is the timing of the execution of the three methods of the spring profile interceptor. The interceptors are
1. PreHandle
Preprocessing callback method, which implements the preprocessing of the processor (such as login check). The third parameter is the response processor (such as the specific Controller implementation).
Return value: true means to continue the process (such as calling the next interceptor or processor); false indicates that the process is interrupted (such as login check failure) and will not continue to call other interceptors or processors. In this case, we need to generate a response through response
2. PostHandle
The post-processing callback method implements the post-processing of the processor (but before rendering the view). At this time, we can process the model data or the view through modelAndView (model and view objects). ModelAndView may also be null.
3. AfterCompletion
Callback method when the whole request is processed, that is, when the view is rendered, for example, in performance monitoring, we can record the end time and output the elapsed time, and we can also clean up some resources, similar to finally in try-catch-finally, but only call the afterCompletion of the interceptor that the preHandle in the processor execution chain returns true.
First, the user request arrives at the front-end controller DispatcherServlet. The front-end controller finds the processor mapper, finds the corresponding processor handler according to the requested method, generates the execution chain of the interceptor and handler execution sequence, and gives it to DispatcherServlet.
DispatcherServlet finds the corresponding processor adapter to process.
Prehandler executes before the request is processed. The return value of this method is of Boolean value Boolean. When it returns false, the request ends, and subsequent Interceptor and Controller will not be executed. When the return value is true, the preHandle method of the next Interceptor will be called. If it is already the last Interceptor, the method in the current requested Controller will be called.
The postHandler method is executed after the current request is processed, that is, after the method call in Controller, but it is called before the DispatcherServlet does the view return rendering, so we can manipulate the ModelAndView object after Controller processing in this method.
AfterCompletion this method will be executed after the entire request is completed, that is, after DispatcherServlet renders the corresponding view, and the main purpose of this method is to do resource cleanup work. Resource releases like exception handling will be put at this step.
The execution order of multiple interceptors is as follows: interceptor A's preHandler-- > interceptor B's preHandler-- > B's postHandler-- > A's postHandler-- > B's afterCompletion-- > A's afterCompletion.
Thank you for reading, the above is the content of "how to use Spring interceptor to monitor the execution time of each Controller or method". After the study of this article, I believe you have a deeper understanding of how to use Spring interceptor to monitor the execution time of each Controller or method, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.