Spring AOP : Pointcut Expression

In this article we are going to discuss in detail AOP pointcut expression with examples. before reading this article, if you want to cover the basics of spring AOP then check article click_here.

Content :

Pointcut expression: Pointcut in spring AOP is defined based on an expression called a pointcut expression. This helps to find a set of joinpoints based on conditions as specified in the expression. these condition helps to target specific packages, methods, classes, annotation, and beans.
Pointcut expression is defined in two parts,
execution(public int *.*.aopdemo.service.MyServiceImpl.getSum(int,int))

  • Pointcut Designator (PCD): PCD (highlighted above in blue color) helps to precisely target method, class, package, beans, and annotation as joinpoint.
  • Expression condition: it (highlighted above in yellow color) helps to identify which specific class, method, bean, or annotation is used as a joinpoint. each PCD has different syntax of expression conditions.

Different PCD and their expression condition syntax.

  • execution : The most commonly used PCD, It's main use to find joinpoint based on method.
    Syntax : modifiers-pattern? return-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?
    Here modifiers-pattern, declaring-type-pattern, and throws-pattern are optional.
    • modifiers-pattern: It specifies the access modifiers of the methods to be matched. e.g., public, protected, private, and * in case of any.
    • return-type-pattern: It specifies the return type of the methods to be matched e.g., void, int, String, Object. * in case of any return type.
    • declaring-type-pattern: It specifies the type or full qualified class name in which the methods are declared, e.g. com.coderstuff01.aopdemo.service.MyServiceImpl and * for any type.
    • name-pattern: It specifies the name of the methods to be matched e.g., getSum and * for any method name.
    • param-pattern: It specifies the parameter types of the methods to be matched e.g., int, String, * for any parameter type and .. for any number of parameters.
    • throws-pattern: It specifies the full qualified class name of exceptions thrown by the methods to be matched e.g., com.coderstuff01.aopdemo.exception.CoderStuffException and * for any exception.
    Example:
    but it can also be used to search joinpoint based on package and class as well. Example:
  • within : It considers all methods as joinpoints within a specified package or class.
    Syntax : within(full qualified class name) Example
  • bean : It considers all methods as joinpoint which is defined in the specified bean.
    Syntax : bean(bean name) Example
    • This considers all methods inside bean named coderstuffservice as joinpoint.
  • target : It refers to the actual object that is instance of the specified class.
    Syntax : target(class qualified name) Example
    • Here com.coderstuff01.aopdemo.service.MyServiceIntf is an interface, and its implementation is provided by com.coderstuff01.aopdemo.service.MyServiceImpl class. so it consider all methods inside com.coderstuff01.aopdemo.service.MyServiceImpl class as joinpoint.
  • annotation : It considers all methods as joinpoint which have specified annotations on top of it.
    Syntax : @annotation(annotation-type-pattern) Example
  • args : It match those method whose number of argument and argument's type matches as specified in args expression.
    Syntax : args(argument type)
    • we can use * to denote any type, .. to denote any number of arguments of any type. For examples
    • args(*,int) : it denotes first argument of any type but second argument of type int.
    • args(..) : it denotes target method can accepts any number of arguments.
    • args() : it denotes target method cannot accepts any argument.
    Example

    Using args we can also access argument values, without using JoinPoint object . Below is an example.
    Here in args we passed names that will hold value of target method (joinpoint) arguments. the type of these arguments we defined in advise method logBeforeMethodOfTargetInstance that is int firstArg, int secondArg.

@Pointcut

In spring AOP, we can also create a named pointcut which we can reuse in our application code. @Pointcut annotation helps to achieve this. Using @Pointcut we can give a name to a pointcut and reuse it by its name. Example :

We can combine multiple pointcut conditions in a single pointcut expression using &&, || and ! Operators. Example :
here it will consider all methods inside the class which is of type com.coderstuff01.aopdemo.service.MyServiceIntf but will not consider method public int com.coderstuff01.aopdemo.service.MyServiceImpl.getSum(int,int).

Comments