Understanding Pointcut and Advisor in Spring AOP

Understanding pointcut and advisor in Spring

The following post teaches you the what is a point cut and advisor in Spring AOP.

What is a pointcut?

A point cut is a piece of code that decides for which methods the advice should be applied. In the previous examples, you have seen before, after and around advices. All of those advises apply for all the methods in the target object.
However, there may be some cases in which you might want the advice to apply only for certain methods. Then a point cut will be helpful.

What is an advisor?

You know what is an advice, it is nothing more than a piece of code that should be executed before or after or around the method but that should not be written in the body of the method.
You know the general meaning of an advisor, the one who advises. Usually the one who advises both gives the advice and tells how to apply it. In a similar way, the advisor in Spring AOP also gives the advice as well as tells for which method(s) the advice should be applied.
In more simple words, an advisor is an object pointing to a point cut and an advice.

In the previous examples, you have seen how we have added an advice using the addAdvice() method of the ProxyFactory class. Here, the advice is applied to all the methods in the target object. Now, we want to filter methods for which the advice should be applied. To do so, we define a point cut.
Instead of adding an advice using the addAdvice() method which applies advice to all the methods, we use the addAdvisor() method which takes in an Advisor object that defines what advice should be applied and to whom it should be applied.

Now whenever you execute a method on the target object, the condition is checked (i.e. whether the method matches the given condition a.k.a point cut). If the condition is satisfied, then the advice is applied to that method.

The Pointcut interface

The Pointcut interface represents a Point cut. This interface contains two methods.
  1. ClassFilter getClassFilter();
  2. MethodMatcher getMethodMatcher();
You will be writing a class that implements this interface and should provide body for these methods in order to create a pointcut. However, there are also some classes that implement this interface which you can extend or use directly to create pointcuts. We will talk about them later. Let me explain the two methods.

The first method should written a ClassFilter object. This filters the class of the target object. The ClassFilter contains a single method
  1. boolean matches(Class cls)
You will need to implement this method and using the Class parameter passed to this method, you will filter the class. For example, you might write this code in matches()

return (cls.getName().equals("MyBean"));

This matches only the class named MyBean.

The next method is MethodMatcher. This matches the method. For example, you will want to filter the method by its name. The MethodMatcher interface contains three methods.
  1. boolean isRuntime()
  2. boolean matches(Method method, Class targetClass)
  3. boolean matches(Method method, Class targetClass, Object[] args)
Now, here we will have to discuss about two types of point cuts. Let's talk about them later. But for now, we will discuss last two methods.

The first method takes in two parameters that Method and Class with which you can filter the methods. For example, using this method you can filter methods by name, their class, the no. of arguments, the types of arguments.

The second method is the same as above method, but you can filter the method by the values passed to the method too. For example, consider a method foo(int x)

Now, you might want the advice to be applied to foo(10) but not foo(20) or foo(anyValueOtherThanTen)
Then, in such cases this will be helpful.
You might have a doubt, if the matches has the Class parameter, then why do we need getClassFilter(). The answer is that, while checking whether an advice should be applied to a method, the getClassFilter() method is executed first before the getMethodMatcher() because if first, the target class satisfies the given condition, then go for checking the method. If the class doesn't, then the there is no need of checking if the method satisfies. This saves a lot of time.

Posted by at on

Tags: Spring AOP,