What is a ProceedingJoinPoint? When is it used?

1. Overview

In this article, we will learn the ProceedingJoinPoint of Spring AOP. We use this type as the first parameter to the Around advice method. This article will focus on ProceedingJoinPoint.

2. ProceedingJoinPoint

The ProceedingJoinPoint is used in Around advice that helps to:

  • Change the method arguments at runtime.
  • Block the target method execution completely.
  • Execute the target method

ProceedingJoinPoint is a subclass of JoinPoint, meaning it inherits all the methods of JoinPoint and also adds the below two methods:

  • proceed(Object[] args)
  • proceed()

The difference between the above two methods lies with how we pass the arguments to the target method. ProceedingJoinPoint encompasses all the arguments passed by the caller. Calling the method will display all the arguments.

The return type of the proceed methods are of Object type as the target method can return any type, so. Whatever result the target method returns, we get it as an Object and return the same to the caller.

4.1.1 proceed()

When you invoke the target method using this proceed method, ProceedingJoinPoint will pass all the arguments to the target method internally with no changes.

In the below example, we have made no changes to the method arguments and just called proceed method.

@Around(execution("@annotation(Logger)"))
public Object logMethodExecution(ProceedingJoinPoint pjp) { 
    Object[] args = pjp.getArgs(); // args passed by the called
    Object obj = pjp.proceed(); // All the args will be passed to the target method by the ProceedingJoinPoint internally
    return obj;
}

The return type of proceed methods are

4.1.1 proceed(Object[] args)

Using this proceed with arguments method, we can change the arguments before passing them to the target method.

The proceed method’s argument Object[] array will be used as the arguments to the method execution when it proceeds.

In the below example, we have changed the method arguments of the target method. customArgs has been passed to the proceed method as an argument. The target method executes by taking this customArgs as argument.

@Around(execution("@annotation(Logger)"))
public Object logMethodExecution(ProceddingJoinPoint pjp) { 
    Object[] customArgs = new Object[] {"Admin"};
    Object obj = pjp.proceed(customArgs); // custom args
    return obj;
}
@Logger
public String sayHelloWorld(String name) {
    return "Hello World " + name;
}

The target method sayHelloWorld executes with the custom value new Object[] "Admin" passed by the Around advice. So, it returns “Hello World Admin” irrespective of the value passed by the actual caller.

3. Conclusion

In this article, we gained knowledge on the ProceedingJoinPoint and its proceed methods with examples. See Spring AOP article to understand more on AOP.

1 thought on “What is a ProceedingJoinPoint? When is it used?”

Leave a Comment