PointCut in Spring
PointCut defines at what Joinpoints Advices has to be applied, instead of defining advices at all join points.
If we want to use PointCuts in AOP based applications then we have to configure that pointcuts in Spring configuration File.
To configure Pointcuts in configuration file then we have to use the following two types of PointCuts.
- Static Pointcut
- Dynamic Pointcut
1. Static Pointcut
Static pointcuts define advice that is always executed. Static pointcuts are based on method and target class, and cannot take into account the method’s arguments. Static pointcuts are sufficient – and best – for most usages. It’s possible for Spring to evaluate a static pointcut only once, when a method is first invoked: after that, there is no need to evaluate the pointcut again with each method invocation.
To represent Pointcuts , Spring framework has provided a predefined interface in the form of “org.springframework.aop.PointCut”.
SpringFramework has provided the following Implementation classes for
2. Dynamic Pointcut
Dynamic pointcuts determine if advice should be executed by examining the runtime method arguments.
Dynamic pointcuts are costlier to evaluate than static pointcuts. They take into account method arguments, as well as static information. This means that they must be evaluated with every method invocation; the result cannot be cached, as arguments will vary.
To represent Dynamic Pointcut Spring has provided the following predefined class.
If we want to use Pointcuts in Spring applications then we have to configure Pointcut and Advisor in apring configuration file.
In spring applications, we will use “DefaultPointCutAdvosor” inorder to suggest the advices to the Pointcuts.
Where if we want to use NameMatchMethodPointcut then we have to use “mappedNames” property of type “array” and we must provide business method names as values to which we want to apply advices.
<bean id="pointcut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedNames"> <array> <value>displayEmployee</value> <value>getEmployeeDetails</value> </array> </property> </bean>
Where If we want to use “Perl5RegexpMethodPointcut” and “JdkRegexpMethodPointcut” in spring applications then we have to use a property like “pattern” of list type with different patterns.
<bean id="pointcut" class="org.springframework.aop.support.Perl5RegexpMethodPointcut"> <property name="patterns"> <list> <value>.*Employee.*</value> </list> </property> </bean>
Where if we want to use DefaultPointcutAdvisor in spring applications then we have to use “pointcut” and “advice” properties.
<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="pointcut"/> <property name="advice" ref="validatorAdvice"/> </bean>