Spring aop aspectJ pointcut expression examples

In this tutorial, I am listing down some examples which will help you to write pointcut expressions to match any kind of method joint points into your spring application. For complete AspectJ pointcut language, please refer to the AspectJ programming guide available on AspectJ’s web site.

1. How to match method signature patterns

The most typical pointcut expressions are used to match a number of methods by their signatures.

1.1. Match all methods within a class in another package

For example, the following pointcut expression matches all of the methods declared in the EmployeeManager interface. The preceding wildcard matches methods with any modifier (public, protected, and private) and any return type. The two dots in the argument list match any number of arguments.

execution(* com.howtodoinjava.EmployeeManager.*(..))

1.2. Match all methods within a class within same package

You can omit the package name if the target class or interface is located in the same package as this aspect.

execution(* EmployeeManager.*(..))

1.3. Match all public methods in EmployeeManager

Use public keyword in start, and use * to match any return type.

execution(public * EmployeeManager.*(..))

1.4. Match all public methods in EmployeeManager with return type EmployeeDTO

Use public keyword and return type in start.

execution(public EmployeeDTO EmployeeManager.*(..))

1.5. Match all public methods in EmployeeManager with return type EmployeeDTO and first parameter as EmployeeDTO

Use public keyword and return type in start. Also, specify your first parameter as well. Rest parameters can be matched through two dots.

execution(public EmployeeDTO EmployeeManager.*(EmployeeDTO, ..))

1.6. Match all public methods in EmployeeManager with return type EmployeeDTO and definite parameters

Use public keyword and return type in start. Also, specify all parameter types as well.

execution(public EmployeeDTO EmployeeManager.*(EmployeeDTO, Integer))

2. How to match class type signature patterns

When applied to Spring AOP, the scope of these pointcuts will be narrowed to matching all method executions within the certain types only.

2.1. Match all methods defined in classes inside package com.howtodoinjava

It’s much like previous example.

 within(com.howtodoinjava.*) 

2.2. Match all methods defined in classes inside package com.howtodoinjava and classes inside all sub-packages as well

For including, sub-packages use two dots.

 within(com.howtodoinjava..*) 

2.3. Match all methods with a class in another package

Much like previous example using execution keyword.

 within(com.howtodoinjava.EmployeeManagerImpl) 

2.4. Match all methods with a class in same package

In case of same package, drop package name.

 within(EmployeeManagerImpl) 

2.5. Match all methods within all all implementing classes of EmployeeManager interface

Use + (plus) sign to match all implementations of an interface.

 within(EmployeeManagerImpl+) 

3. How to match class name patterns

You can match all beans as well having a common naming pattern e.g.

3.1. Match all methods defined in beans whose name ends with ‘Manager’.

It’s quite easy one. Use an * to match anything preceding in bean name and then matching word.

 bean(*Manager) 

4. How to combine pointcut expressions

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

4.1. Match all methods with names ending with Manager and DAO

Use '||' sign to combine both expressions.

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

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

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

11 thoughts on “Spring aop aspectJ pointcut expression examples”

  1. 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
  2. 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
  3. 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
  4. 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

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.