Detailed examples of Java interceptor and aspect in the application market, java instances
I believe you are familiar with the concepts of interceptor and cut-plane. In this article, we will look at the use of interceptor and cut-plane in the application market.
Use of interceptor: each time before receiving a request, the method in this interceptor will be called. If the preHandle method returns true, it indicates 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 handle T Ion {// request. getMethod: get, post, and other if ("OPTIONS ". equals (request. getMethod () {// specify to allow other domain names to access response. setHeader ("Access-Control-Allow-Origin", "*"); // response type response. setHeader ("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); // set the response header to response. setHeader ("Access-Control-Allow-Headers", "Content-Type, x-requested-with, X-Custom-Header"); response. setStatus (204); return true;} // get the result 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, response, object handler, Exception ex) throws Exception {}}
Use of Slice:
// Bad bypass notification: The ProceedingJoinPoint type parameter must be included. // the whole process of the surround notification is similar to that of the dynamic Proxy: the parameter of the ProceedingJoinPoint type can determine whether to execute the target method // and the surround notification must have a return value. 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 ); // pre-notification, indicating that the code before this will call result = pjd before calling controller. proceed (); recordOprationLog (result, methodName, result); // post-Notification logger.info ("Arround: The method" + methodName + "ends");} catch (Throwable e) {e. printStackTrace (); // logger is notified of an exception. error ("Arround: The method" + methodName + "occurs exception:" + e); // throw new RuntimeException (e); // if no exception is thrown, if an exception occurs, it is caught and executed. The result is returned. The result value is null and converted to int. // The logger.info ("Arround: the method "+ methodName +" ends with the Result "+ result); return result ;}
Summary
The above is all the details about the Java interceptor and slice instances in the application market, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!