Spring 4 Join point to get method argument names and values

Debopam picture Debopam · Jun 19, 2017 · Viewed 19.5k times · Source

I am using Spring 4.3. Is it possible to get method parameter names and values passed to it? I believe this can be done using AOP (before advice) if possible could you please give me a source code.

Answer

snorbi picture snorbi · Mar 7, 2018

The following works as expected (Java 8 + Spring 5.0.4 + AspectJ 1.8.13):

@Aspect
@Component
public class SomeAspect {

    @Around("@annotation(SomeAnnotation)")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();

        System.out.println("First parameter's name: " + codeSignature.getParameterNames()[0]);
        System.out.println("First argument's value: " + joinPoint.getArgs()[0]);

        return joinPoint.proceed();
    }
}