M02 Q07 Syntax and types of pointcut expressions
Pointcut designator types supported by Spring AOP:
- execution
- within
- args
- bean
- this
- target
- @annotation
- @args
- @within
- @target
execution
Pointcut designator β execution
β matches method execution
General Form:
execution([visibility modifiers] [return type] [package].[class].[method]([arguments]) [throws exceptions]
Description:
[visibility modifiers]
β public/protected, if omitted all are matched, can be used with negation, for example!protected
[return type]
β void, primitive or Object type, cannot be omitted, can be used with wildcard*
, can be used with negation, for example!int
[package]
β package in which class is located, may be omitted if a class is located within the same package as aspect, wildcard*
may be used to match all packages, wildcard..
may be used to match all sub-packages[class]
β Class name to match against, may be omitted, may be used with*
wildcard, matches subclasses of the class as well[method]
β Name of the method, whole or partial method name can be used with*
wildcard[arguments]
β Maybe empty to match methods without any arguments, may be used with wildcard..
to match zero or more arguments, may be used with wildcard*
to match all types of a specific argument, may be used with!
Negation[throws exceptions]
β Match method that throws exceptions from the given list, can be used with negation!
within
Pointcut designator β within
β matches execution within specified class/classes, optionally you can specify class package.
General Form: within([package].[class])
Description:
[package]
β package where a class is located, may be used with..
wildcard (includes all sub-packages) or with*
wildcard, may be omitted[class]
β class against which match should happen, may be used with*
wildcard
args
Pointcut designator β args
β matches execution of a method with matching arguments.
General Form:
args([parametertype1, parametertype2, ..., parameter_ttypeN])
Description:
[parameterpeN]
β simple or object type, maybe *
to indicate one parameter of any type, maybe ..
to indicate zero or more arguments, you can specify the type with the package
bean
Pointcut designator β bean
β matches execution of a method with matching Spring Bean Name
General Form: bean([beanName])
Description:
[beanName]
β the name of the Spring Bean (automatically generated by framework, or set manually),
this
Pointcut designator β this
β matches execution against the type of proxy that was generated by Spring AOP
General Form: this([type])
Description:
[type]
β a type of the proxy, matches if a generated proxy is of the specified type
target
Pointcut designator β target
β matches execution against the type of the target object invoked by proxy
General Form: target([type])
Description:
[type]
β a type of the target object invoked by proxy, matches if the target object is of the specified type
According to the type of proxy (JDK Proxy or CGLIB Proxy), this
and target
can have a different meaning.
@annotation
Pointcut designator β @annotation
β matches method execution annotated with specified annotation
General Form: @annotation([annotationtype])
Description:
[annotationtype]
β a type of annotation used to the annotated method which should match pointcut expression
@args
Pointcut designator β @args
β matches method execution with the argument, which types (classes) are annotated with specified annotation type, note that class should be annotated, not the argument of the method itself
General Form: @args([annotationtype])
Description:
[annotationtype]
β a type of annotation used on top of the class, which represents the type of argument
We defined @CustomValidation
and use it on the top of Person
class.
And Person
class is used in the savePerson(Person person)
method.
That means savePerson
method will match because it uses Person
class in the arguments, which is annotated by @CustomValidation.
@within
Pointcut designator β @within
β matches method executions inside classes annotated with specified annotation
General Form: @within([annotationtype])
Description:
[annotationtype]
β a type of annotation used on top of the class, inside which method execution should be matched
@target
Pointcut designator β @target
β matches method executions inside proxied target class that is annotated with a specific annotation
General Form: @target([annotationtype])
Description:
[annotationtype]
β a type of annotation used on top of the proxied class, inside which method execution should be matched
What would be the correct pointcut expression to match both getter and setter methods?
Pointcut expressions can be combined together with usages of logical operators:
!
β negation||
- logical or&&
- logical and
To match getters and setter, execution Pointcut Expression can be used together with the ability to combine them with logical operators. The expression that will match getters and setter can look like this: