In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "SpringBoot@Aspect print access request and return data way", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's train of thought slowly in depth, together to study and learn "SpringBoot@Aspect print access request and return data way"!
Catalogue
SpringBoot@Aspect print access request and return data
Aspect: the first way
Aspect: the second way
SpringBoot @ Aspect Annotation details
1. Add maven dependency annotations
2. Add AOP class
3. Set tangent points
4. Configure pre-notification
5. Configure post return notification
6. Post exception notification
7. Post final notice
8. Surround notification
SpringBoot@Aspect print access request and return data
Why do you use aspect? using aspect makes logging faceted, which reduces the coupling of the code. There are two ways to log the input and output data, as follows:
Aspect: the first way
@ Before and @ AfterReturning to slice the controller.
Output data:
Aspect: the second way
@ Around to slice the controller.
Output data:
Both methods are able to log monitor the request data.
The first method is different from the second method. The second method uses @ Around to surround the data. The joinPoint.proceed () returns data needs to be executed before the following code can be executed. This is a blocking request, so it is more appropriate to use the first method.
SpringBoot @ Aspect annotation details 1, add maven dependency annotation org.springframework.boot spring-boot-starter-aop 2, add AOP class @ Component@Aspectpublic class JournalServiceAspect {} 3, set tangent point / * * tangent point * / private final String POINT_CUT = "execution (* com.xx.xx..* (..)" @ Pointcut (POINT_CUT) private void pointcut () {} 4, configure pre-notification / * * pre-notification, and be called * @ param joinPoint * / @ Before (value = POINT_CUT) public void before (JoinPoint joinPoint) {logger.info ("pre-notification") before the method is called; / / get the parameter information of the target method Object [] obj = joinPoint.getArgs () / / AOP proxy class information joinPoint.getThis (); / / proxy target object joinPoint.getTarget (); / / signature of the most notifications used Signature signature = joinPoint.getSignature (); / / which method is the proxy logger.info ("which method is the proxy" + signature.getName ()) / / AOP proxy class name logger.info ("name of AOP proxy class" + signature.getDeclaringTypeName ()); / / AOP proxy class (class) information signature.getDeclaringType (); / / get RequestAttributes RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes (); / / get HttpServletRequest information HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference (RequestAttributes.REFERENCE_REQUEST) from get RequestAttributes / / if you want to get Session information, you can write: / / HttpSession session = (HttpSession) requestAttributes.resolveReference (RequestAttributes.REFERENCE_SESSION); / / get the request parameter Enumeration enumeration = request.getParameterNames (); Map parameterMap = Maps.newHashMap (); while (enumeration.hasMoreElements ()) {String parameter = enumeration.nextElement (); parameterMap.put (parameter,request.getParameter (parameter)) } String str = JSON.toJSONString (parameterMap); if (obj.length > 0) {logger.info ("the requested parameter information is:" + str);}}
* * Note: JoinPoint and RequestContextHolder are used here.
1). The signature information of the notification can be obtained through JoinPoint, such as target method name, target method parameter information and so on.
2) obtain request information and Session information through RequestContextHolder. **
5. Configure Post return Notification / * * Post return Notification * Note here: * if the first parameter in the parameter is JoinPoint, the second parameter is the information of the return value * if the first parameter in the parameter is not JoinPoint Then the first parameter is the corresponding parameter * returning in returning: it defines that post return notification can be performed only if the return value of the target method is the corresponding parameter type of the notification method, otherwise it will not be executed. * for the notification method parameter corresponding to returning, the Object type will match any target return value * @ param joinPoint * @ param keys * / @ AfterReturning (value = POINT_CUT,returning = "keys") public void doAfterReturningAdvice1 (JoinPoint joinPoint,Object keys) {logger.info ("the return value of the first post return notification:" + keys) } @ AfterReturning (value = POINT_CUT,returning = "keys", argNames = "keys") public void doAfterReturningAdvice2 (String keys) {logger.info ("the second post returns the return value of the notification:" + keys) } 6. Post exception notification / * * post exception notification * defines a name that matches a parameter name of the notification implementation method. When the target method throws an exception and returns, the exception thrown by the target method is passed to the notification method. * throwing: post-exception notification can only be performed when the exception thrown by the target method and the corresponding parameter exception type of the notification method, otherwise it will not be executed. * for throwing, the notification method parameter of Throwable type will match any exception. * @ param joinPoint * @ param exception * / @ AfterThrowing (value = POINT_CUT,throwing = "exception") public void doAfterThrowingAdvice (JoinPoint joinPoint,Throwable exception) {/ / Target method name: logger.info (joinPoint.getSignature (). GetName ()); if (exception instanceof NullPointerException) {logger.info ("null pointer exception!") }} 7. Post final notification / * Post final notification (the post notification method will be executed as soon as the target method is finished) * @ param joinPoint * / @ After (value = POINT_CUT) public void doAfterAdvice (JoinPoint joinPoint) {logger.info ("Post final notification has been executed!!") 8. Surround notification / * surround notification: * surround notification is very powerful, which can determine whether the target method is executed, when, whether the method parameter needs to be replaced, and whether the return value needs to be replaced after execution. * the first parameter of the surround notification must be of type org.aspectj.lang.ProceedingJoinPoint * / @ Around (value = POINT_CUT) public Object doAroundAdvice (ProceedingJoinPoint proceedingJoinPoint) {logger.info ("the target method name of the surround notification:" + proceedingJoinPoint.getSignature (). GetName ()); try {Object obj = proceedingJoinPoint.proceed (); return obj;} catch (Throwable throwable) {throwable.printStackTrace () } return null;} Thank you for your reading. The above is the content of "how SpringBoot@Aspect prints access requests and returns data". After the study of this article, I believe you have a deeper understanding of the way SpringBoot@Aspect prints access requests and return data, 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.