1. Overview
Have you ever wondered what is the JoinPoint argument used for? In this article, we will understand the JoinPoint argument in Spring and also its use.
2. JoinPoint argument in Spring
Before moving on to the topic, let’s understand the Join Point.
A JoinPoint is a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, it always represents the method execution. In short, A join point refers to the target method execution.
If you want to retrieve the Join point information inside the advice body, then you had to declare a parameter of type org.aspectj.lang.JoinPoint
in your advice method. A JoinPoint argument is an object to retrieve information about a Joinpoint inside the advice method.
The first parameter of the advice method should be JoinPoint to make it work.
Below advice methods supports the JoinPoint argument:
- After advice
- Before advice
- After returning advice
- After throwing advice
Besides the above advice methods, the Around advice should have the first parameter of type ProceedingJoinPoint, which is a subclass of JoinPoint. See ProceedingJoinPoint for more information.
You can use this Join point information for tracing and logging your applications
2.1. JoinPoint Argument Example
Let’s see an example to understand and go through the information available with JoinPoint.
- getArgs - the method arguments
- getThis - the proxy object. This will always be the same object as that matched by the
this
pointcut designator. - getTarget - the target object. This will always be the same object as that matched by the
target
pointcut designator. - getSignature - a description of the target method that is being advised
- toString - a useful description of the target method being advised
- kind - Kind of Join point. In Spring, it is always of kind “method-execution” as we support advice only for method execution
You can refer this and target pointcut designators to understand this and target pointcut designators.
@Component @Aspect public class LoggingAspect { @Before("execution(* com..Utility.formatData(..))") public void beforeAdviceMethod(JoinPoint joinPoint) { System.out.println("beforeAdviceMethod <- start"); System.out.println("Join Point static part = " + joinPoint.getStaticPart()); System.out.println("Signature = " + joinPoint.getSignature()); System.out.println("Args = " + Arrays.toString(joinPoint.getArgs())); System.out.println("Kind = " + joinPoint.getKind()); System.out.println("Join Point Long String " + joinPoint.toLongString()); System.out.println("Join Point Short String " + joinPoint.toShortString()); System.out.println("Join Point String " + joinPoint.toString()); System.out.println("Join Point getTarget " + joinPoint.getTarget()); System.out.println("Join Point getThis " + joinPoint.getThis()); System.out.println("beforeAdviceMethod <- stop\n\n"); } } // calling the target method Utility utility = context.getBean(Utility.class); utility.formatData("John", "Doe");
Result:
beforeAdviceMethod <- start Join Point static part = execution(String com.tedblob.Utility.formatData(String,String)) Signature = String com.tedblob.Utility.formatData(String,String) Args = [John, Doe] Kind = method-execution Join Point Long String execution(public java.lang.String com.tedblob.Utility.formatData(java.lang.String,java.lang.String)) Join Point Short String execution(HelloBean.formatData(..)) Join Point String execution(String com.tedblob.Utility.formatData(String,String)) Join Point getTarget com.tedblob.Utility@6b695b06 Join Point getThis com.tedblob.Utility@6b695b06 beforeAdviceMethod <- stop
3. Conclusion
In this article, you have seen the JoinPoint argument along with the example. You can use the JoinPoint information for logging and tracing.