Joinpoint VS ProceedingJoinPoint in AOP using aspectJ?
Asked Answered
Y

4

24

Can any one tell me what is the difference between Joinpoint and Proceedingjoinpoint?

When to use Joinpoint and Proceedingjoinpoint in the method of aspect class?

I used the JoinPoint in my AspectJ class like:

@Pointcut("execution(* com.pointel.aop.test1.AopTest.beforeAspect(..))")  
public void adviceChild(){}  

@Before("adviceChild()")  
public void beforeAdvicing(JoinPoint joinPoint /*,ProceedingJoinPoint pjp - used refer book marks of AOP*/){ 

    //Used to get the parameters of the method !
    Object[] arguments = joinPoint.getArgs();
    for (Object object : arguments) {
        System.out.println("List of parameters : " + object);
    }

    System.out.println("Method name : " + joinPoint.getSignature().getName());
    log.info("beforeAdvicing...........****************...........");
    log.info("Method name : " + joinPoint.getSignature().getName());
    System.out.println("************************"); 
}

But what I see in other resources is:

@Around("execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")
public void aroundAddAdvice(ProceedingJoinPoint pjp){
    Object[] arguments = pjp.getArgs();
    for (Object object : arguments) {
        System.out.println("Book being added is : " + object);
    }
    try {
        pjp.proceed();
    } catch (Throwable e) {
        e.printStackTrace();
    }
} 

Here what will ProceedingJoinPoint do differently compare to 'JointPoint? Also what willpjp.proceed()` do for us?

Youlandayoulton answered 3/4, 2013 at 7:28 Comment(0)
P
13
@Around("execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")

It means before calling com.mumz.test.spring.aop.BookShelf.addBook method aroundAddAdvice method is called. After System.out.println("Book being added is : " + object); operation is completed . it will call your actual method addBook(). pjp.proceed() will call addBook() method.

Phlegmy answered 3/4, 2013 at 8:35 Comment(6)
Thans for your reply.. So Are you saying the pjp.proceed() will call the addBook() method twice ?Youlandayoulton
No, first it will call aroundAddAdvice method.Then it will call addBook().Phlegmy
You can see it by debugging.Phlegmy
So What is the difference between the Joinpoint and Proceedingjoinpoint ?Youlandayoulton
.If you use before,after-throwing,after-returning and after use Jointpoint.If you use around use Proceedingjoinpoint .They are just interface.Proceedingjoinpoint is extending Joinpoint .So,seeing its functionality you can select as per urs requirement which to use.Phlegmy
let us continue this discussion in chatYoulandayoulton
S
33

An around advice is a special advice that can control when and if a method (or other join point) is executed. This is true for around advices only, so they require an argument of type ProceedingJoinPoint, whereas other advices just use a plain JoinPoint. A sample use case is to cache return values:

private SomeCache cache;

@Around("some.signature.pattern.*(*)")
public Object cacheMethodReturn(ProceedingJoinPoint pjp){
    Object cached = cache.get(pjp.getArgs());
    if(cached != null) return cached; // method is never executed at all
    else{
        Object result = pjp.proceed();
        cache.put(pjp.getArgs(), result);
        return result;
    }
}

In this code (using a non-existent cache technology to illustrate a point) the actual method is only called if the cache doesn't return a result. This is the exact way the Spring EHCache Annotations project works, for example.

Another specialty of around advices is that they must have a return value, whereas other advice types must not have one.

Sextant answered 3/4, 2013 at 9:39 Comment(4)
Thanks for your golden answer...One more doubt. How to get the input parameters of a method and the return value of the same method using the @Around using the ProceedingJoinPoint ?Youlandayoulton
See my code above: the input parameters can be retrieved through pjp.getArgs(), the method return value through pjp.proceed()Sextant
Lets click the link for chat chat.stackoverflow.com/rooms/27495/…Youlandayoulton
@Anand I don't have the time to chat here. look at my profile and contact me though skype or fb chatSextant
P
13
@Around("execution(* com.mumz.test.spring.aop.BookShelf.addBook(..))")

It means before calling com.mumz.test.spring.aop.BookShelf.addBook method aroundAddAdvice method is called. After System.out.println("Book being added is : " + object); operation is completed . it will call your actual method addBook(). pjp.proceed() will call addBook() method.

Phlegmy answered 3/4, 2013 at 8:35 Comment(6)
Thans for your reply.. So Are you saying the pjp.proceed() will call the addBook() method twice ?Youlandayoulton
No, first it will call aroundAddAdvice method.Then it will call addBook().Phlegmy
You can see it by debugging.Phlegmy
So What is the difference between the Joinpoint and Proceedingjoinpoint ?Youlandayoulton
.If you use before,after-throwing,after-returning and after use Jointpoint.If you use around use Proceedingjoinpoint .They are just interface.Proceedingjoinpoint is extending Joinpoint .So,seeing its functionality you can select as per urs requirement which to use.Phlegmy
let us continue this discussion in chatYoulandayoulton
U
4

Use JoinPoint with following advice types:

 @Before, @After, @AfterReturning, @AfterThrowing

Use ProceedingJoinPoint with following advice type:

@Around
Usually answered 5/8, 2020 at 12:36 Comment(0)
N
0

ProceedingJoinPoint is a JoinPoint with additional features.

ProceedingJoinPoint is used with @Around advice. @Around is very powerful advice that combines the features of rest of the Advice.

ProceedingJoinPoint::proceed is basically used to execute the original method.

Consider following example:

@Around("@annotation(com.annotations.ExecutionLoggerAround)")
public void executeLogger(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("Method execution starts at :: " + System.currentTimeMillis());
    proceedingJoinPoint.proceed();
    System.out.println("Method execution completes at :: " + System.currentTimeMillis());
}

@ExecutionLoggerAround
public void generateReport() {
    System.out.println("Generating XLS report !!");
}

public @interface ExecutionLogger {
}

Now when you will call generateReport, Around advice will be executed. If you skip line proceedingJoinPoint.proceed(), the actual method will not be executed. You will see only those two println in the console.

PS: To understand better you need to know how AOP works. Spring creates proxy objects either using JDK proxy or CGLIB proxy. So its actually the proxied object that executes at runtime which use our method behind the scene.

I have written more about this here

Nonobedience answered 29/8, 2022 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.