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?
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.
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:
Joinpoint a specific point in the program execution, such as the execution of a method or handling of an exception.Where the control flow can arrive via two different paths. (IMO : that's why call joint).
Advice The action taken by an aspect at a particular joinpoint.
Pointcut is an expression that matches joinpoints and determines whether advice should be applied.
syntax:
Advice("execution(modifiers? return-type declaring-type? method-name(param) throws?) ")
Example: @Before("execution(public Integer com.company.service..add(..))")