Understanding the Pointcut expressions
Pointcut is an expression language of spring AOP which is basically used to match the target methods to apply the advice.
It has two parts ,one is the method signature comprising of method name and parameters.
Other one is the pointcut expression which determines exactly which method we are applying the advice to.
Example
- @Pointcut("execution(* SimpleCalculator.*(..))") // the pointcut expression
- private void doSomething() {} // the pointcut signature
@Pointcut("execution(* SimpleCalculator.*(..))") // the pointcut expression private void doSomething() {} // the pointcut signature
The name of the pointcut expression is doSomething().
It will be applied on all the methods of SimpleCalculator class regardless of its parameters and return type.
The syntax of pointcut expression should be
execution(access_specifier package_name class_name method_name (arguments_list))
Above things can be specified more specifically or can be specified as “*” to indicate wildcard matching anything
“*” indicates access specifier could be private,public ,protected or default
For Example :
To specify access specifier as public, we use public keyword , to specify anything as access specifier we can use “*”.
Some examples of common pointcut expressions are given below.
The execution of any public method:
- execution(public * *(..))
execution(public * *(..))
The execution of any method with a name beginning with “calculate”
- execution(* calculate*(..))
execution(* calculate*(..))
The execution of any method defined by the SimpleCalculator class
- execution(* com.kb. SimpleCalculator.*(..))
execution(* com.kb. SimpleCalculator.*(..))
The execution of only public method defined by the SimpleCalculator class
- execution(public com.kb. SimpleCalculator.*(..))
execution(public com.kb. SimpleCalculator.*(..))
The execution of any method defined in the com.kb package
- execution(* com.kb.*.*(..))
execution(* com.kb.*.*(..))
The execution of any method defined in the com.kb package or a sub-package
- execution(* com.kb..*.*(..))
execution(* com.kb..*.*(..))
Any join point (method execution only in Spring AOP) within the com.kb package
- within(com.kb.*)
within(com.kb.*)
Any join point (method execution only in Spring AOP) within the com.kb package or a sub-package
- within(com.kb..*)
within(com.kb..*)
Any join point (method execution only in Spring AOP) which takes a single parameter, and where the argument passed at runtime is Serializable
- args(java.io.Serializable)
args(java.io.Serializable)
Note that the pointcut given in this example is different to
- execution(* *(java.io.Serializable))
execution(* *(java.io.Serializable))
The args version matches if the argument passed at runtime is Serializable,
the execution version matches if the method signature declares a single parameter of type Serializable.
any join point (method execution only in Spring AOP) where the executing method has an @Transactional annotation
- @annotation(org.springframework.transaction.annotation.Transactional)
@annotation(org.springframework.transaction.annotation.Transactional)
any join point (method execution only in Spring AOP) on a Spring bean named “simpleCalc”
- bean(simpleCalc)
bean(simpleCalc)
any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression ‘*service’
- bean(*service)
bean(*service)