java - 使用 AOP 的自定义注解传递方法参数
问题描述
我已经定义了注释
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
当我在要考虑的方法下使用自定义注释时,然后我想获取方法签名的参数(它们是对象,而不是字符串,int ant 字节)。
有没有简单的方法通过 AOP 的自定义注释来获取方法参数?
解决方案
一个简单的演示可以:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodTimer {
}
方面处理程序:
@Aspect
@Slf4j
@Component
public class TimeCounterAspect {
@Around("@annotation(methodTimer)")
public Object logMethodRequests(ProceedingJoinPoint joinPoint, MethodTimer methodTimer)
throws Throwable {
Long start = System.currentTimeMillis();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
String methodName = method.getName();
Object[] myArgs = joinPoint.getArgs();
Object obj = null;
try {
obj = joinPoint.proceed();
} catch (Exception e) {
throw e;
} finally {
log.info("Retrieving timeCost: {} ms in Method: {} args: {}",
System.currentTimeMillis() - start, methodName, Arrays.deepToString(myArgs));
}
return obj;
}
}
推荐阅读
- ios - 获取屏幕大小的结果以显示在 Objective-C 的文本字段中
- python - python selenium href地址太长无法获取正确数据
- javascript - 使用两个不同的 Firestore 字段执行操作
- javascript - 如何使这个打字稿映射功能变得不那么复杂?
- git - 为什么我的 github url 权限在我推送时被拒绝?
- r - fviz_cluster,更改 pt 的颜色以反映键
- regex - 正则表达式查找(/替换)字符串中的多个字符实例
- python - 带有串行端口的 Python 线程等待其他线程完成
- java - 使用 java 下载 Blob 给了我一个巨大的文件
- sql - 在这种情况下,如何从存储过程中返回正确的开始日期值?