1. Overview
A Pointcut expression in Spring AOP matches the target methods using various patterns to execute the advice. In this article, we will discuss pointcut expression patterns available in Spring AOP.
See Pointcut in Spring AOP and Pointcut designators article for more information on pointcuts and pointcut designators.
The format of a Pointcut expression is:
pointcut-designator(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
‘?’ at the end shows that the part is optional and you can omit it if not needed.
- modifiers-pattern? (optional) – Access modifiers like public, protected, private.
- ret-type-pattern – Method’s return type
- declaring-type-pattern? (optional) – Package or class
- name-pattern – Name of the method
- param-pattern – Method parameters
- throws-pattern? (optional) – Method throwing this exception
2. Modifiers pattern
This is an optional part, and it is used to filter methods using the access modifiers (public, protected, private). Let’s see an example to match the execution of any public method.
execution(public * *(..))
The below example matches the execution of any protected method.
execution(protected * *(..))
3. Return type pattern
It is used to match the method using its return type. You can use * to match any return type. The below expression will match the methods with any return type inside the com.tedblob.mod
el.Manager class.
execution(* com.tedblob.model.Manager.*(..))
A fully qualified type name will match only when the method returns the specified type. The below expression matches the methods that return String inside the com.tedblob.model.Manager
class.
execution(java.lang.String com.tedblob.model.Manager.*(..))
4. Declaring type pattern
This is an optional part and matches using the package or class.
com.tedblob.model.*
applies to all classes in the package com.tedblob.model
execution(* com.tedblob.model.*(..))
com.tedblob.model.Manager.*
applies only to Manager class inside the package com.tedblob.model
execution(* com.tedblob.model.Manager.*(..))
* matches all classes inside all packages
execution(* *(..))
com.tedblob.model..* applies to all classes in the package com.tedblob.model or its sub-packages
5. Name pattern
This pattern filters the method using its name. * to match methods with any name. The below expression matches methods with any name inside the class com.tedblob.model.Manager
.
execution(* com.tedblob.model.Manager.*(..))
The below expression will match only methods starting with the name “set” inside the class com.tedblob.model.Manager
.
execution(* com.tedblob.model.Manager.set*(..))
6. Param Pattern
This pattern filters the method using its parameters.
() matches a method that takes no parameters. The below expression applies to methods with no parameters and only setter methods will match.
execution(* com.tedblob.model.Manager.*())
(..) matches many parameters (zero or more parameters).
execution(* com.tedblob.model.Manager.*(..))
(*) matches a method taking one parameter of any type whereas (*, String) matches a method taking two parameters, the first can be of any type, the second must be a String.
execution(* com.tedblob.model.Manager.*(*, String)) execution(* com.tedblob.model.Manager.*(*))
7. Throws Pattern
This helps to filter methods depending on what exception it throws. This expression will match a method that throws java
.lang.NumberFormatException inside com.tedblob.model.Manager
class.
execution(* com.tedblob.model.Manager.*(..) throws java.lang.NumberFormatException
@Before(value = "execution(* com.tedblob.model.Manager.*(..) throws java.lang.NumberFormatException public void beforeNumberFormatException(JoinPoint joinPoint) { System.out.println(joinPoint.getSignature().getName() + "::NumberFormatException"); }
public int calculateValue(String number) throws java.lang.NumberFormatException { try { return Integer.parseInt(number); } catch (NumberFormatException nfe) { throw new NumberFormatException("Unable to convert string to number"); } }
If the above method is present in the com.tedblob.model.Manager
class, then the advice beforeNumberFormatException
will run whenever you invoke this calculateValue
method.
8. Conclusion
In this article, we discussed the various pointcut expressions available in Spring AOP.
Pingback: Pointcut in Spring AOP (pointcuts, expressions, designators) - TedBlob
Pingback: Pointcut designators (execution, within, this, target, args) Spring - TedBlob