Spring Builders

Cover image for Spring AOP Pointcut expressions
Rajitha Gunawardhane
Rajitha Gunawardhane

Posted on

Spring AOP Pointcut expressions

In Spring AOP (Aspect-Oriented Programming), pointcut expressions are used to define and match points in the execution of a program where an aspect can be applied. These points include method executions, object instantiation, and handling of exceptions. Understanding and using these expressions effectively is crucial for defining the scope of where advice should be applied in your application.
You can read here a simple explanation of SpringAOP

Structure of Method Matching Joincut Expressions
Join point expressions in Spring AOP are based on the AspectJ pointcut expression language. Below is the syntax of method execution matching expression used in Spring AOP.

Syntax:

execution(modifiers-pattern? return-type-pattern declaring-type-pattern? method-name-pattern(param-pattern) throws-pattern?)

Example:
execution(* com.example.service.*.*(..))

The execution() join point expression in Spring AOP is quite flexible and powerful, allowing you to define which method executions should be intercepted by advice. Each part of the syntax has its own role in specifying which methods to match.

Each part of this expression specifies certain characteristics of the methods to be matched. Let's break down what each segment means:

modifiers-pattern?:

Description: Specifies the access modifier of the method (e.g., public, protected).
Optional: The ? indicates that this part is optional.
Example: public matches only public methods.
Wildcard: Using * matches methods with any modifier.

return-type-pattern:

Description: Specifies the return type of the method.
Example:

  • * matches methods with any return type.
  • void matches methods that return nothing.
  • String matches methods that specifically return a String. Wildcard: * means any return type.

declaring-type-pattern?:

Description: Specifies the class or package in which the method is declared.
Optional: The ? indicates that this part is optional.
Example:

  • com.example.service.* matches any class in the com.example.service package.
  • com.example.service.OrderService matches methods in the OrderService class specifically.

method-name-pattern:

Description: Specifies the name of the method.
Example:

  • * matches any method name.
  • get* matches methods with names starting with get.
  • processOrder matches the processOrder method specifically.
  • Wildcard: * is used to match any name pattern.

param-pattern:

Description: Specifies the types and number of parameters that the method accepts.
Examples:

  • () matches methods with no parameters.
  • (String) matches methods with a single String parameter.
  • (..) matches methods with any number of parameters of any type.
  • (String, int) matches methods with a String followed by an int.

throws-pattern?:

Description: Specifies any exceptions that the method might throw.
Optional: This part is optional and often omitted in basic join point expressions.

Example with All Parts

execution(public * com.example.service.OrderService.processOrder(String, ..) throws IOException)

  • Access Modifier: public
  • Return Type: Matches any return type (*)
  • Declaring Type: com.example.service.OrderService
  • Method Name: processOrder
  • Parameters: String followed by any number of additional parameters ((..))
  • Throws Clause: throws IOException

Top comments (0)