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

1
2
@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:

1
execution(public * *(..))
execution(public * *(..))

The execution of any method with a name beginning with “calculate”

1
execution(* calculate*(..))
execution(* calculate*(..))

The execution of any method defined by the SimpleCalculator class

1
execution(* com.kb. SimpleCalculator.*(..))
execution(* com.kb. SimpleCalculator.*(..))

The execution of only public method defined by the SimpleCalculator class

1
execution(public  com.kb. SimpleCalculator.*(..))
execution(public  com.kb. SimpleCalculator.*(..))

The execution of any method defined in the com.kb package

1
execution(* com.kb.*.*(..))
execution(* com.kb.*.*(..))

The execution of any method defined in the com.kb package or a sub-package

1
execution(* com.kb..*.*(..))
execution(* com.kb..*.*(..))

Any join point (method execution only in Spring AOP) within the com.kb package

1
within(com.kb.*)
within(com.kb.*)

Any join point (method execution only in Spring AOP) within the com.kb package or a sub-package

1
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

1
args(java.io.Serializable)
args(java.io.Serializable)

Note that the pointcut given in this example is different to

1
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

1
@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”

1
bean(simpleCalc)
bean(simpleCalc)

any join point (method execution only in Spring AOP) on Spring beans having names that match the wildcard expression ‘*service’

1
bean(*service)
bean(*service)

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on