Understanding Pointcut and Advisor in Spring AOP
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.
The first method should written a ClassFilter object. This filters the class of the target object. The ClassFilter contains a single method
boolean matches(Class cls)
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.
boolean matches(Method method, Class targetClass)
boolean matches(Method method, Class targetClass, Object args)
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
matcheshas 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.