'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;
}
Solution 1:[1]
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.
Solution 2:[2]
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();
.
Solution 3:[3]
@Around("someMethod()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("around - before - " + joinPoint.getSignature());
try {
System.out.println("Inside Try block of around");
return joinPoint.proceed();
} finally {
System.out.println("around - after - " + joinPoint.getSignature());
}
}
@Before("someMethod()")
public void before(JoinPoint joinPoint) {
System.out.println("before - " + joinPoint.getSignature());
}
/*Consider above methods are matched to a pointcut for some joinpoint. If we didn't
use joinPoint.proceed() in @Around advice then @Before advice will not be called.
Output with joinPoint.proceed()
-around - before - " + joinPoint.getSignature()
-"Inside Try block of around";
-"before - " + joinPoint.getSignature()
-around - after - " + joinPoint.getSignature()
Output without joinPoint.proceed()
-around - before - " + joinPoint.getSignature()
-"Inside Try block of around";
-around - after - " + joinPoint.getSignature()
*/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Community |
Solution 2 | |
Solution 3 | Vashista Basava |