Spring AOP Pointcut Expression Syntax with Examples

Learn Spring AOP Pointcut expression syntax and see a few examples such as pointcut expressions for specific methods or all methods in a class or package.

Spring AOP (Aspect-Oriented Programming) is a powerful paradigm to modularize cross-cutting concerns in applications. The pointcut is a set of one or more join points where advice should be applied. A join point is a point during the execution of a program, such as method execution, object instantiation, or field access. Pointcuts define the when and where of advice execution.

In this article, we’ll learn the intricacies of Spring AOP Pointcut expression syntax and see a few examples helping us write precise and effective pointcuts.

1. Pointcut Expression Syntax

Spring AOP uses AspectJ-style expressions for defining pointcuts. The syntax involves combining various elements to precisely target specific join points.

For example, use execution() to specify the method execution join points. The basic syntax is in the pattern ‘execution(modifiers? return_type method_name(param_type1, param_type2, …))‘. Consider the following example:

execution(public void com.example.service.MyService.doSomething())

Use wildcards to match multiple elements, similar to regular expressions. For instance, * matches any sequence of characters, and .. matches any number of parameters. Consider the following example:

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

Use within() to specify join points within a certain type or package. For example, the following expression matches all methods within the ‘com.example.service‘ package.

within(com.example.service.*)

For more finer details on the syntax, continue reading. We will discuss a few most commonly used pointcut expressions.

2. Pointcut Expressions: Matching Specific Methods in a Class

The most typical point-cut expressions are used to match the methods by their signatures. Let us see a few most commonly used patterns.

Pointcut ExpressionDescription
execution(* com.example.EmployeeManager.*(..))Match all methods in the specified package and class
execution(* EmployeeManager.*(..))Match all methods in the same package and specified class
execution(public * EmployeeManager.*(..))Match all public methods in EmployeeManager
execution(public Employee EmployeeManager.*(..))Match all public methods in EmployeeManager with return type Employee object
execution(public Employee EmployeeManager.*(Employee, ..))Match all public methods in EmployeeManager with return type Employee and first parameter as Employee
execution(public Employee EmployeeManager.*(Employee, Integer))Match all public methods in EmployeeManager with return type Employee and the specified parameters

3. Pointcut Expressions: Matching All Methods in a Class or Package

We can intercept the execution of all the methods in a class or package using the within() function. Let us see a few most commonly used patterns.

Pointcut ExpressionDescription
within(com.example.*)Match all methods in all the classes inside package ‘com.example.*
within(com.example..*)Match all methods in all the classes inside package ‘com.howtodoinjava‘ and the classes inside all sub-packages as well
within(com.example.EmployeeManagerl)Match all methods within the specified class in the specified package
within(EmployeeManager)Match all methods within the specified class in the current package
within(IEmployeeManager+)Match all methods within all the implementations of the specified interface

4. Pointcut Expressions: Matching Class Name Patterns

We can use the bean() function to match all the methods in all the classes that match the specified pattern.

Pointcut ExpressionDescription
bean(*Manager)Match all methods in bean whose name ends with ‘Manager
bean(employeeManager)Match all methods in specified bean with name ‘employeeManager
bean(com.example.service.*)Match all methods in all the beans in a specific Package
bean(@MyCustomAnnotation *)Match all methods in all the beans with a specific annotation

5. Combining Pointcut Expressions

In AspectJ, pointcut expressions can be combined with the operators && (and), || (or), and ! (not).

Let us understand this with a simple example. The following matches all the methods in beans whose names ending with Manager or DAO.

Use '||' sign to combine both expressions.

 bean(*Manager) || bean(*DAO) 

I hope that the above information will help you when you face any difficulty in determining the correct pointcut expression in your application.

6. Conclusion

Spring AOP Pointcut expression syntax is pretty much similar to the regular expressions when we use wildcards. By understanding the intricacies of method signatures, wildcards, type matching, and annotations, we can craft precise and targeted Pointcuts.

Happy Learning !!

Leave a Comment

  1. Can we use a generic expression like ResponseEntity<abc> in pointcut

    @After(“execution(public ResponseEntity<abc> com.backend.data_access.controller.ControllerV1..*(..))”)

    Reply
  2. Great post, thank you!

    I have an interesting corner case when I am using the expression like:
    “@Pointcut(“!within(org.springframework.data.repository.Repository+)”
    because I want to exclude ALL methods in ALL classes implementing Repository Spring interface. When a Java project uses Repository (which is the case 99% of Java project) then everything worked fine, However, if a project does NOT use Repository then my Aspect break at init time and Spring gives me an error that it cannot initialize a bean because it could not locate the Repository type…
    Look, I am using NOT (please, see exclamation point in my expression) in my expression thus I assumes that Spring should be smart enough to ignore the logic locating the type Repository if I said NOT…
    Is there a way to fix this somehow?
    Thank you!
    Alex Gordon

    Reply
  3. How can I include multiple packages in the pointcut expression ?
    e.g Take the logger example in above mentioned comment. I want the logger around
    all methods in packages inside the folder com.example.packages

    For one package I can use “execution(* com.example.packages.Class1)”

    will “execution(* com.example.packages.*)” also work ?

    Reply
    • You need to use: within(com.howtodoinjava..*). See section ‘Matching all methods defined in classes inside package com.howtodoinjava and classes inside all sub-packages as well’

      Reply
  4. Hi Lokesh,

    I am using @After advice in my Aspect. The problem I am facing is, all of my methods from the REST Controller are called except only one. I tried lot of things but that specific method is not called in Aspect.
    I have used aspect advice as follows.

    @After(“execution(* package.Class.methodName (..))”)

    Could you please help me know where am I going wrong ?
    Thanks in advance.

    Reply
  5. Hi Lokesh,
    I am trying to use aspectJ for logging. There are two things I am trying to do
    1. Log entry and exit of every method called Configured using

    @Around("execution(public * package..*.*(..))")
    

    2. Log execution time for methods having LogTime annotation. Configured using

    @Around("execution(public * package..*.*(..)) || @annotation(logTime)")
    

    I have created two around advises for this. Now these two advises itself are cross cutting.
    I need to log entry and exit for methods having LogTime annotation also.
    Only first advise is getting triggered in my case.
    Could you please help me know if I am doing anything wrong?
    Thanks in advance.

    Reply

Leave a Comment

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.