我用
定义了注释
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
当我在要表达方面的方法下使用自定义注释时,然后我想获取方法签名的参数(它们是Object,而不是字符串,整数字节)。
是否有一种简单的方法来获取带有AOP自定义注释的方法参数?
答案 0 :(得分:1)
一个简单的演示可以是:
@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;
}
}
答案 1 :(得分:0)
您可以通过ProceedingJoinPoint
访问参数:
@Around("execution(@com.path.annotation.YourAnnotation * *(..)) && @annotation(annotation)")
public Object execute(final ProceedingJoinPoint pjp, final YourAnnotation annotation) throws Throwable {
Object result = pjp.proceed();
// Here is the method arguments
Object[] args = pjp.getArgs();
return result;
}