Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joinpoint VS ProceedingJoinPoint in AOP using aspectJ?

Tags:

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?

like image 579
Human Being Avatar asked Apr 03 '13 07:04

Human Being


People also ask

What is difference between ProceedingJoinPoint and JoinPoint?

ProceedingJoinPoint is an extension of the JoinPoint that exposes the additional proceed() method. When invoked, the code execution jumps to the next advice or to the target method. It gives us the power to control the code flow and decide whether to proceed or not with further invocations.

What is the JoinPoint argument used for?

A JoinPoint argument is an object that can be used to retrieve additional information about join point during execution. JoinPoint needs to be the first parameter of Advice, only, in that case, Spring Framework will inject JoinPoint into advice method.

What is the difference between JoinPoint and Pointcut?

A Joinpoint is a point in the control flow of a program where the control flow can arrive via two different paths(IMO : that's why call joint). A Pointcut is a matching Pattern of Joinpoint i.e. set of join points.


2 Answers

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.

like image 61
Sean Patrick Floyd Avatar answered Oct 17 '22 02:10

Sean Patrick Floyd


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

like image 35
abishkar bhattarai Avatar answered Oct 17 '22 02:10

abishkar bhattarai