跳转到主要内容

Spring AOP 切面中获取包名和类名的方法

Avatar emanjusaka
统计中…
245
0
Spring AOP 切面中获取包名和类名的方法

在切面中获取到被切入的类名和包名的方法:

 package top.emanjusaka.aop;
 ​
 import org.aspectj.lang.ProceedingJoinPoint;
 import org.aspectj.lang.annotation.Around;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Pointcut;
 import org.aspectj.lang.reflect.MethodSignature;
 import org.springframework.stereotype.Component;
 ​
 /**
  * @Author emanjusaka
  * @Date 2024/7/31 15:07
  * @Version 1.0
  */
 @Aspect
 @Component
 public class TestAspect {
 ​
     // 定义切入点
     @Pointcut("execution(* top.emanjusaka.aop.*.*(..))")
     public void testCut() {
 ​
     }
 ​
     @Around("testCut()")
     public Object doGetPackageName(ProceedingJoinPoint joinPoint) throws Throwable {
         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
         Class<?> targetClass = signature.getDeclaringType();
         System.out.println("类名:" + targetClass.getSimpleName());
         System.out.println("包名:" + targetClass.getPackage().getName());
         System.out.println("包名 + 类名:" + targetClass.getName());
         return joinPoint.proceed();
     }
 }
 ​

本文由 emanjusaka 创作,发表于 www.emanjusaka.com,采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

❋

微信扫码分享

微博
点赞是美意,赞赏是鼓励。如有帮助,可以关注下微信公众号。
微信 微信
支付宝 支付宝
查看赞赏名单 →
评论
← 上一篇 把 GeoJson 转成 GeometryCollection 类型 下一篇 → MybatisPlus 的 wrapper 方法格式化日期查询数据