Spring AOP
June 23, 2020

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: