• AOP进阶-连接点


    连接点

    • 在Spring中用JoinPoint抽象了连接点,用它可以获取方法执行时的相关信息,如目标类名、方法名、方法参数等
      • 对于@Around通知,获取连接点信息只能使用 ProceedingJoinPoint
      • 对于其它四种通知,获取连接点信息只能使用JoinPoint,它时是ProceedingJoinPoint的父亲类型
      • 具体代码如下
          1. package com.example.tlias.AOP;
          2. import lombok.extern.slf4j.Slf4j;
          3. import org.aspectj.lang.JoinPoint;
          4. import org.aspectj.lang.ProceedingJoinPoint;
          5. import org.aspectj.lang.annotation.Around;
          6. import org.aspectj.lang.annotation.Aspect;
          7. import org.aspectj.lang.annotation.Before;
          8. import org.aspectj.lang.annotation.Pointcut;
          9. import org.springframework.stereotype.Component;
          10. import java.util.Arrays;
          11. @Component
          12. @Aspect
          13. @Slf4j
          14. public class TestJoinPoint {
          15. @Pointcut("execution(* com.example.tlias.service.DeptLogService.*(..))")
          16. public void PointCut() {
          17. }
          18. @Before("PointCut()")
          19. public void before(JoinPoint joinPoint) {
          20. log.info("TestJointPoint...before...");
          21. // 1.获取目标对象类名
          22. String ClassName = joinPoint.getTarget().getClass().getName();
          23. log.info("目标对象类名{}", ClassName);
          24. // 2.获取目标对象方法名
          25. String Methodname = joinPoint.getSignature().getName();
          26. log.info("目标对象的方法名{}", Methodname);
          27. // 3.获取目标方法运行时传入的参数
          28. Object[] args = joinPoint.getArgs();
          29. log.info("目标方法时传入的参数{}", Arrays.toString(args));
          30. }
          31. @Around("PointCut()")
          32. public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
          33. log.info("TestJointPoint...around...");
          34. // 1.获取目标对象类名
          35. String ClassName = proceedingJoinPoint.getTarget().getClass().getName();
          36. log.info("目标对象的类名是{}", ClassName);
          37. // 2.获取目标对象方法名
          38. String MethodName = proceedingJoinPoint.getSignature().getName();
          39. log.info("目标对象的方法名{}", MethodName);
          40. // 3.获取目标方法运行时传入的参数
          41. Object[] args = proceedingJoinPoint.getArgs();
          42. log.info("目标方法运行时传入的参数{}", Arrays.toString(args));
          43. // 4.放行目标方法执行
          44. Object result = proceedingJoinPoint.proceed();
          45. // 5.获取目标方法的返回值
          46. log.info("目标方法运行的返回值{}", result);
          47. log.info("TestJointPoint around after....");
          48. return result;
          49. }
          50. }

  • 相关阅读:
    MES系统是如何解决工厂上的难题的?
    【小白学YOLO】YOLOv3网络结构细致解析
    千变万化的Promise
    [附源码]Python计算机毕业设计 家乡旅游文化推广网站
    ThreadPoolExecutor详解
    数据加密标准DES安全性
    VBA技术资料MF52:VBA_在Excel中突出显示前 10 个值
    3D Gaussian Splatting for Real-Time Radiance Field Rendering
    【Dolphinscheduler3.1.1】二次开发本地启动项目(前端+后端)
    【AGC】AppGallery Connect SDK遇到url is null问题分析
  • 原文地址:https://blog.csdn.net/weixin_64939936/article/details/132721306