HowToDoInJava

  • Java 8
  • Regex
  • Concurrency
  • Best Practices
  • Spring Boot
  • JUnit5
  • Interview Questions
  • Dark Mode

Spring aop aspectJ pointcut expression examples

By Lokesh Gupta | Filed Under: Spring AOP

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

TwitterFacebookLinkedinRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

11
Leave a Reply

This comment form is under antispam protection
8 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
9 Comment authors
This comment form is under antispam protection
  Subscribe  
newest oldest most voted
Notify of
Zeljko

I thought Spring AOP can’t advise private methods? 1.1 states the opposite. Am I missing something?

Vote Up0Vote Down  Reply
1 day ago
Lokesh Gupta

Spring AspectJ based AOP supports it. Try it to verify.

Vote Up0Vote Down  Reply
9 hours ago
Alex

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

Vote Up0Vote Down  Reply
10 months ago
Shash

Very useful. Thank you so much.

Vote Up0Vote Down  Reply
1 year ago
Utkarsh Gautam

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 ?

Vote Up0Vote Down  Reply
2 years ago
Lokesh Gupta

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’

Vote Up0Vote Down  Reply
2 years ago
Aniruddha Joshi

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.

Vote Up0Vote Down  Reply
2 years ago
Hrishikesh Joshi

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.

Vote Up0Vote Down  Reply
3 years ago
Lokesh Gupta

Try :
execution(@LogTime * *.*(..))

Also, you may want to try changing the || operator to && operator.

Vote Up0Vote Down  Reply
3 years ago
Arijeet Saha

Thanks for such a helpful info for aop expressions.

Vote Up0Vote Down  Reply
3 years ago
Binh Thanh Nguyen

Thanks, very nice examples.

Vote Up0Vote Down  Reply
4 years ago

Search Tutorials

Spring AOP Tutorial

  • AOP – Introduction
  • AOP – Annotation Config
  • AOP – XML Config
  • AOP – @Before
  • AOP – @After
  • AOP – @Around
  • AOP – @AfterReturning
  • AOP – @AfterThrowing
  • AOP – Before Advice
  • AOP – After Advice
  • AOP – Around Advice
  • AOP – After-Returning Advice
  • AOP – After-Throwing Advice
  • AOP – Pointcut Expressions
  • AOP – Aspects Ordering
  • AOP – Transactions
  • AOP – Interview Questions

Popular Tutorials

  • Java 8 Tutorial
  • Core Java Tutorial
  • Collections in Java
  • Java Concurrency
  • Spring Boot Tutorial
  • Spring AOP Tutorial
  • Spring MVC Tutorial
  • Spring Security Tutorial
  • Hibernate Tutorial
  • Python Tutorial
  • Jersey Tutorial
  • Maven Tutorial
  • Log4j Tutorial
  • Regex Tutorial

Meta Links

  • Advertise
  • Contact Us
  • Privacy policy
  • About Me

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2016 · HowToDoInjava.com · All Rights Reserved. | Sitemap

wpDiscuz