Spring AOP Pointcut Expression Syntax with Examples

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 !!

Comments

Subscribe
Notify of
guest
12 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

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.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode