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.
In Spring AOP, a join point always represents a method execution. Join point information is available in advice bodies by declaring a parameter of type org.aspectj.lang.JoinPoint. We’ll now learn about join points, and how we can use arguments in the advice methods to get information about join points.
This is possible. See my answer on this page. In your AOP advice you can use methods of the JoinPoint to get access to methods and their parameters. There are multiple examples online and at stackoverflow.
To access the argument, we should provide a JoinPoint argument to the advice: This PCD limits matching to join points within types that have the given annotation: This PCD limits matching to join points where the subject of the join point has the given annotation. For example, we can create a @Loggable annotation:
In this article, we saw how to get all the available information about a method using a Spring AOP aspect. We did that by defining a pointcut, printing out the information into the console, and checking the results of running the tests. The source code for our application is available over on GitHub.
CodeSignature methodSignature = (CodedSignature) joinPoint.getSignature();
String[] sigParamNames = codeSignature.getParameterNames();
You can get method signature arguments names.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With