[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint

Time:2024-5-15

1. Preamble

In Spring AOP, JoinPoint and ProceedingJoinPoint are both key interfaces for getting information about methods in a cutover and controlling method execution. Their main difference lies in the way they are used and their functionality in AOP notifications.

2. Introduction to JoinPoint

A Joinpoint is an important concept in aspect-oriented programming (AOP) that refers to a specific point in the execution of an application that can be intercepted. In AOP, a Joinpoint represents a specific point in the execution of a program, such as a method call, an exception thrown, etc. AOP frameworks intercept these Joinpoints to insert additional logic to achieve the functionality of the cross-cutting point of interest.

We can use JoinPoint to get all the information except the exception parameter object and return value.

Example.
[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint
You can see that JoinPoint has many methods. We can use breakpoint debugging to see what these methods do.
Add the JoinPoint parameter to the method’s arguments
[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint
Hit the breakpoint, debugging, you can see the parameters related to JoinPoint.
[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint
[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint
Here you can debug the corresponding methods[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint
Example.
[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint
Of course, if you feel the trouble, you can also directly write code for debugging, such as.
[Spring]AOP advanced -JoinPoint and ProceedingJoinPoint
The corresponding methods of JoinPoint can be debugged in this way. For reasons of space, we won’t demonstrate this here.

3. Obtaining information about the enhanced method

Commonly used methods are as follows.

  1. Get the parameters passed in when the method is called.

    Object[] args = joinPoint.getArgs();
  2. Get the notified target object

    Object target = joinPoint.getTarget();
  3. Getting information about a proxy method

    MethodSignature signature = (MethodSignature) joinPoint.getSignature(); // Get proxy method information
    String methodName = signature.getName(); // Get the method name
    Class<? > returnType = signature.getReturnType(); // Get the return type
    Class<? >[] parameterTypes = signature.getParameterTypes(); // Gets an array of parameter types

In this example, we first use the joinPoint.getSignature() method to get the information of the proxy method and then convert it to an object of type MethodSignature. Then we can use the MethodSignature object’s method to get the method name, return type, parameter type and other information for processing in the notification.

  1. Getting the return value in an enhancement method

    @AfterReturning(value = "point()",returning = "ret")
    public void methodAfterReturning(JoinPoint joinPoint, Object ret){
        System.out.println("AfterReturning");
    }
  2. Getting the exception object in an enhancement method

    @AfterThrowing(value = "point()",throwing = "e")
    public void methodAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("AfterThrowing");
    }

Note the way it is written in the annotation, with some parameters added, as well as the assignment of the target method to thevalueIn addition to obtaining the return value of the enhancement method, not necessarily to Object type, according to the needs of their own adjustments

In a nutshell, Joinpoint represents the point of program execution in AOP that can be intercepted, and by intercepting these Joinpoints the functionality of the cross-cutting concern is achieved. It is very important to understand and use Joinpoint in Spring’s AOP.

Unlike the above JoinPoint which is used for methods that add the four annotations @Before,@AfterReturning,@After,@AfterThrowing, wrap-around notifications use theProceedingJoinPoint

4. ProceedingJoinPoint

In Spring AOP, ProceedingJoinPoint is a sub-interface of JoinPoint, which is specialized for Around advice.ProceedingJoinPoint contains execution information of the notified method, and you can access the information and parameters of the notified method. You can use the ProceedingJoinPoint interface to implement more flexible and fine-grained around the notification logic.

5. Obtaining information about the surround notification method

The ProceedingJoinPoint interface provides the following common methods:

  1. proceed(): this is the most important method in the ProceedingJoinPoint interface. In a wrap-around notification, calling the proceed() method continues the execution of the notified method. If the proceed() method is not called in a wrap-around notification, the notified method will not be executed.
@Around("execution(* com.example.service.*.*(..))")
public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable {
    // Execute preemptive logic
    Object result = joinPoint.proceed(); // continue execution of the notified method
    // Execute postback logic
    return result;
}

take note ofjoinPoint.proceed()method is possible to have a return value, the return value type may be many, can be set directly to Object type

  1. getArgs(): get the array of arguments of the notified method.
Object[] args = joinPoint.getArgs();
  1. getTarget(): get the target object to be notified.
Object target = joinPoint.getTarget();

The main purpose of the ProceedingJoinPoint interface is to control the execution of the notified method in a wrap-around notification, as well as to access information and parameters of the notified method. Developers can use the ProceedingJoinPoint interface to implement more flexible and fine-grained logic for wrap-around notifications.

6. Summary

JoinPoint is used to get information about a method, while ProceedingJoinPoint can control the execution of a method in addition to getting information about the method, which makes it very useful in wrap-around notifications. In the practice of AOP, developers need to understand how these two interfaces are used and choose the right interface to realize the functionality of the cross-cutting concern point according to the specific needs.