Reputation: 176
I have defined annotation with
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
When I use custom annotation under method which i want to aspect, and then i want to get the parameters(they are Object, not string, int ant byte) of the method signature.
is there simple way to get method parameter with custom annotation of AOP?
Upvotes: 0
Views: 2297
Reputation: 7828
A simple demo can as:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodTimer {
}
And the aspect handler:
@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;
}
}
Upvotes: 1
Reputation: 18235
You can access the arguments via 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;
}
Upvotes: 0