Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring AOP: What's the difference between JoinPoint and PointCut?

I'm learning Aspect Oriented Programming concepts and Spring AOP. I'm failing to understand the difference between a Pointcut and a Joinpoint - both of them seem to be the same for me. A Pointcut is where you apply your advice and a Joinpoint is also a place where we can apply our advice. Then what's the difference?

An example of a pointcut can be:

@Pointcut("execution(* * getName()") 

What can be an example of a Joinpoint?

like image 289
Saurabh Patil Avatar asked Mar 16 '13 08:03

Saurabh Patil


People also ask

What is Joinpoint and pointcut in AOP?

In Spring AOP, a join point always represents a method execution. A pointcut is a predicate that matches the join points, and the pointcut expression language is a way of describing pointcuts programmatically.

What is the use of pointcut in Spring?

Pointcut: Pointcut is expressions that are matched with join points to determine whether advice needs to be executed or not. Pointcut uses different kinds of expressions that are matched with the join points and Spring framework uses the AspectJ pointcut expression language.

What is pointcut in AspectJ?

AspectJ provides primitive pointcuts that capture join points at these times. These pointcuts use the dynamic types of their objects to pick out join points. They may also be used to expose the objects used for discrimination. this(Type or Id) target(Type or Id)

What is the difference between Spring AOP and AspectJ AOP?

Spring AOP aims to provide a simple AOP implementation across Spring IoC to solve the most common problems that programmers face. On the other hand, AspectJ is the original AOP technology which aims to provide complete AOP solution.


1 Answers

Joinpoint: A joinpoint is a candidate point in the Program Execution of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a field being modified. These are the points where your aspect’s code can be inserted into the normal flow of your application to add new behavior.

Advice: This is an object which includes API invocations to the system wide concerns representing the action to perform at a joinpoint specified by a point.

Pointcut: A pointcut defines at what joinpoints, the associated Advice should be applied. Advice can be applied at any joinpoint supported by the AOP framework. Of course, you don’t want to apply all of your aspects at all of the possible joinpoints. Pointcuts allow you to specify where you want your advice to be applied. Often you specify these pointcuts using explicit class and method names or through regular expressions that define matching class and method name patterns. Some AOP frameworks allow you to create dynamic pointcuts that determine whether to apply advice based on runtime decisions, such as the value of method parameters.

The following image can help you understand Advice, PointCut, Joinpoints. enter image description here

Source

Explaination using Restaurant Analogy: Source by @Victor

When you go out to a restaurant, you look at a menu and see several options to choose from. You can order one or more of any of the items on the menu. But until you actually order them, they are just "opportunities to dine". Once you place the order and the waiter brings it to your table, it's a meal.

Joinpoints are options on the menu and Pointcuts are items you select.

A Joinpoint is an opportunity within code for you to apply an aspect...just an opportunity. Once you take that opportunity and select one or more Joinpoints and apply an aspect to them, you've got a Pointcut.

Source Wiki:

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

Advice describes a class of functions which modify other functions

A Pointcut is a matching Pattern of Joinpoint i.e. set of join points.

like image 68
Premraj Avatar answered Sep 30 '22 07:09

Premraj