I believe everyone is familiar with the concepts of interceptors and sections. In this article, we will look at some of the usage of interceptors and sections in the application market.
Use of interceptor: Each time a request is received, the method in this interceptor will be called. If the preHandle method returns true, it means that the corresponding controller will continue to be called. If return false,
public class CheckLoginInterceptor implements HandlerInterceptor {private Logger logger = Logger.getLogger(CheckLoginInterceptor.class);private static String TOKEN_VALID_MSG ;static {TOKEN_VALID_MSG=JsonUtil.writeObject2JSON(new AMSResultVO(CodeNum.TOKEN_VALID, CodeMessage.TOKEN_VALID));}public Boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {//request.getMethod get request is get, post, etc. if ("OPTIONS".equals(request.getMethod()))) {// Specify that other domain names are allowed to access response.setHeader("Access-Control-Allow-Origin", "*");// Response type response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");// Response header set response.setHeader("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header");response.setStatus(204);return true;}// Get the data obtained from the header String userName = request.getHeader(CommonConsts.PARAM_USER_NAME);String userToken = request.getHeader(CommonConsts.PARAM_USER_TOKEN);Boolean result = true;String method = request.getRequestURI();if(method.equals("/ams/fileUpload")) {return true;}if(StringUtil.isEmpty(userName) || StringUtil.isEmpty(userToken)) {result = false;} else {result = TokenUtil.validToken(userName, userToken);}// token verification failed if(!result) {response.setContentType("text/html;charset=UTF-8");response.getWriter().print(TOKEN_VALID_MSG);response.getWriter().flush();response.getWriter().close();}return result;}public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}}Use of sections:
//Bad circumcision notification: You need to carry parameters of ProceedingJoinPoint type //The whole process of surround notification is similar to the dynamic proxy: Parameters of ProceedingJoinPoint type can determine whether to execute the target method //The surround notification must have a return value, and the return value is the return value of the target method. @Around("execution(* com.sowell.controller.*Controller.*(..))") public Object aroundMethod(ProceedingJoinPoint pjd) {Object result = null;String methodName = pjd.getSignature().getName();Object args = Arrays.asList(pjd.getArgs());//Execute the target method try {logger.info("request channels begin, param{pageNum:" + methodName + ", pageSize:" + args);//Previous notification, indicating that the code before this will call result = pjd.proceed();recordOprationLog(result, methodName, result);//Post notification logger.info("Arround:The method "+ methodName+" ends");}catch (Throwable e) {e.printStackTrace();//Exception notification logger.error("Arround:The method "+ methodName+"occur exception:"+e);//throw new RuntimeException(e);//If the exception is not thrown, the exception will be caught by the above, and then execute it, return result, the result value is null, converted to int}//Return notification logger.info("Arround:The method "+ methodName+" ends with the Result "+ result);return result;}Summarize
The above is the full explanation of the detailed explanation of the use examples of Java interceptors and sections in the application market. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!