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

96,428

Solution 1

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.

Solution 2

To understand the difference between a join point and pointcut, think of pointcuts as specifying the weaving rules and join points as situations satisfying those rules.

In below example,

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

Pointcut defines rules saying, advice should be applied on getName() method present in any class in any package and joinpoints will be a list of all getName() method present in classes so that advice can be applied on these methods.

(In case of Spring, Rule will be applied on managed beans only and advice can be applied to public methods only).

Solution 3

Layman explanation for somebody who is new to the concepts AOP. This is not exhaustive, but should help in grasping the concepts. If you are already familiar with the basic jargon, you can stop reading now.

Assume you have a normal class Employee and you want to do something every time these methods are called.

class Employee{
    public String getName(int id){....}
    private int getID(String name){...}
}

these methods are called JoinPoints. We need a way to identify these methods so that the framework can find the methods, among all the classes.methods it has loaded. So we will write a regular expression to match the signature of these methods. While there is more to it as you will see below, but loosely this regular expression is what defines Pointcut. e.g.

* * mypackage.Employee.get*(*)

First * is for modifier public/private/protected/default. Second * is for return type of the method.

But then you also need to tell two more things:

  1. When should an action be taken - e.g Before/After the method execution OR on exception
  2. What should it do when it matches (maybe just print a message)

The combination of these two is called Advice.

As you can imagine, you would have to write a function to be able to do #2. So this is how it might look like for the basics.

Note: For clarity, using word REGEX instead of the * * mypackage.Employee.get*(*). In reality the full expression goes into the definition.

@Before("execution(REGEX)")
public void doBeforeLogging() {....}   <-- executed before the matching-method is called

@After("execution(REGEX)")
public void doAfterLogging() {....}  <-- executed after the matching-method is called

Once you start using these quite a bit, you might end up specifying many @After/@Before/@Around advices. The repeated regular expressions will eventually end up making things confusing and difficult to maintain. So what we do, we just give a name to the expression and use it everywhere else in the Aspect class.

@Pointcut("execution(REGEX)") <-- Note the introduction of Pointcut keyword
public void allGetterLogging(){} <-- This is usually empty

@Before("allGetterLogging")
public void doBeforeLogging() {....}

@After("allGetterLogging")
public void doAfterLogging() {....}

BTW, you would also want to wrap this whole logic in a class, that is called Aspect and you would write a class:

@Aspect
public class MyAwesomeAspect{....}

To get all these things to work, you would have to tell Spring to parse the classes to read, understand and take action on the @ AOP keywords. One way to do it is specifying the following in the spring config xml file:

<aop:aspectj-autoproxy>

Solution 4

JoinPoints: These are basically places in the actual business logic where you wish to insert some miscellaneous functionality that is necessary but not being part of the actual business logic. Some examples of JoinPints are: method call, method returning normally, method throwing an exception, instantiating an object, referring an object, etc...

Pointcuts: Pointcuts are something like regular expressions which are used to identify joinpoints. Pontcuts are expressed using "pointcut expression language". Pointcuts are points of execution flow where the cross-cutting concern needs to be applied. There is a difference between Joinpoint and Pointcut; Joinpoints are more general and represents any control flow where we 'may choose to' introduce a cross-cutting concern while pointcuts identifies such joinpoints where 'we want to' introduce a cross-cutting concern.

Solution 5

Comparing an AOP language like AspectJ to a data query language like SQL,

  • you can think of joinpoints (i.e. all places in your code where you can weave aspect code) as a database table with many rows.
  • A pointcut is like a SELECT stamement which can pick a user-defined subset of rows/joinpoints.
  • The actual code you weave into those selected places is called advice.
Share:
96,428
Saurabh Patil
Author by

Saurabh Patil

Seasoned Software Engineer with 10+ years in the industry working primarily on Java tech stack in the e-commerce domain. Have worked with on core products of ebay at it's San Jose headquarters for a few years. Java. JEE. Spring. REST/SOAP. Microservices. Maven. Mongo DB. Oracle. Git. Clearcase.

Updated on July 08, 2022

Comments

  • Saurabh Patil
    Saurabh Patil almost 2 years

    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?