Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does joinPoint.proceed() do?

It is my first approaching to AOP. I've a spring-boot application with one Aspect, a Logger. Searching i reach to the conclusion that the @Around method executes before, and after the method (I'm calling it just in one method), is this right? And in the middle of my @Around method y have a joinPoint.proceed(). If I'm not wrong, the JoinPoint is the object i must use to obtain information of the method where the aspect is getting called but i can't understand what is the proceed actually doing!

This is my code:

@Around(value = "execution(* *(..)) && @annotation(Loggable)", argNames = "ProceedingJoinPoint, Loggable")
public Object logAround(ProceedingJoinPoint joinPoint, Loggable loggable) throws Throwable {

    String methodArguments = loggable.printMethodArguments() ? Arrays.toString(joinPoint.getArgs()) : "[]";

    long start = System.currentTimeMillis();
    Object returnObject = joinPoint.proceed(); // continue on the
                                                // intercepted method
    long elapsedTime = System.currentTimeMillis() - start;

    String returnValue = loggable.printReturn() && returnObject != null ? returnObject.toString() : "[]";

    LOG.info("Logging method: {}.{} Method arguments: {}. Method return value: {}. Method execution time: {}",
            joinPoint.getTarget().getClass().getName(), joinPoint.getSignature().getName(), methodArguments,
            returnValue, elapsedTime);
    return returnObject;
}
like image 702
Nicolas Villacorta Avatar asked Aug 03 '26 15:08

Nicolas Villacorta


2 Answers

As you said, when you're using @Around it's just like you can do whatever you want before the method, then invoke the method, then you can do whatever you want after the method called.

//Read file, Log , .... (Before method calling)
//Invoke the method (joinPoint.proceed)
//Write to the file, complete log, .... (After method calling)

The invoking phase done by joinPoint.proceed().


Log Example

1- Log before calling method

2- Call or invoke the method you set pointcut on it (proceed)

3- Save the log into the database or write it to the file or send it ,...

Authorization Example

In this sample, using @Around, you can authorize users and determine that they can use the method or not?

So you need to do authorization process before the method invocation, if authorization is true then invoke method if not throws an exception or you can log.

1- Authorize user before calling method

2- If Authorization was true then invoke method (joinPoint.proceed();)


In summary, joinPoint.proceed(); means that you are calling the set method, or invoking it.

like image 91
Mehrdad HosseinNejad Yami Avatar answered Aug 05 '26 07:08

Mehrdad HosseinNejad Yami


As you mentioned, the @Around advice surrounds a joinpoint, e.g. method invocation. It can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution.

In your case, the actual method invocation (those method containing a very useful business logic!) happens because of joinPoint.proceed();.

like image 43
Sergii Zhevzhyk Avatar answered Aug 05 '26 08:08

Sergii Zhevzhyk