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

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.

Leave a Comment