Search code examples
javaspringaspectjspring-aop

Joinpoint VS ProceedingJoinPoint in AOP using aspectJ?


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?


Solution

  • @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.