Spring AOP

Declaring Pointcut Expressions with Examples

In this tutorial, I will explain how to declare pointcut expressions to matching any kind of method for join points for Spring beans in the spring application. As we know Spring AOP only supports method execution join points for Spring beans. So in this tutorial we have listed pointcut expressions only those, are matching the execution of methods on Spring beans. A pointcut declaration has four parts as below:

  • Matching Method Signature Patterns
  • Matching Type Signature Patterns
  • Matching Bean Name Patterns
  • Combining Pointcut Expressions

Supported Pointcut Designators by Spring AOP

AspectJ framework supports many Designators but Spring AOP supports only some of them as below:

  • execution – pointcut expression for matching method execution join points.
  • within – pointcut expression for matching to join points within certain types
  • this – pointcut expression for matching to join points where the bean reference is an instance of the given type
  • target – pointcut expression for matching to join points where the target object is an instance of the given type
  • args – pointcut expression for matching to join points where the arguments are instances of the given types
  • @target – pointcut expression for matching to join points where the class of the executing object has an annotation of the given type
  • @args – pointcut expression for matching to join points where the runtime type of the actual arguments passed have annotations of the given type
  • @within – pointcut expression for matching to join points within types that have the given annotation
  • @annotation – pointcut expression for matching to join points where the subject of the join point has the given annotation

 

Declaring pointcut Expressions

1. Matching Method Signature Patterns

Matching all public methods in TransferService
Use public keyword in start, use * to match any return type.

@Pointcut("execution(public * com.doj.app.service.TransferService.*(..))")
public void anyPublicMethodOfTrasferService();

Matching any public methods
Use public keyword in start, use * to match any return type and use another * to match any method name.

@Pointcut("execution(public * *(..))")
public void anyPublicMethod();

Matching any method defined in the service package
Use * in start to match any return type, use second * to match any class name and use another * to match any method name.

@Pointcut("execution(* com.doj.app.service.*.*(..))")
public void anyMethodInServicePackage();

Matching any method defined in the service package or it’s sub-packages
Use * in start to match any return type, use two dots after service package means it’s include sub-packages as well, use second * to match any class name and use another * to match any method name.

@Pointcut("execution(* com.doj.app.service..*.*(..))")
public void anyMethodInServicePackageAndSubPackage();

Matching all public methods in TransferService with return type Account
Use public keyword in start and use Account as a return type.

@Pointcut("execution(public Account com.doj.app.service.TransferService.*(..))")
public void allPublicMethodOfTransferServiceReturnTypeAccount();

Matching all methods in TransferService with return type void and first parameter as Account
Use void keyword in start and use Account as a argument type for first parameter.

@Pointcut("execution(void com.doj.app.service.TransferService.*(Account account, ..))")
public void allMethodOfTransferServiceVoidReturnTypeFirstArgumentAccount();

Matching all public methods in any class of the service package with any return type and method name should be transfer with taking two parameters of Account types
Use public keyword in start and use * as any return type, use another * for any class in the service package, use transfer method name with two parameters of Account Types.

@Pointcut("execution(public * com.doj.app.service.*.tranfer(Account account1, Account account2))")
public void allTranferMethodsInServicePackageWithTwoArgumentsOfAccountType();

2. Matching Type Signature Patterns

Method execution only in Spring AOP. It provides narrowed to matching all method executions within the certain types only.

Matching all methods defined in classes inside package com.doj.app.service

@Pointcut("within(com.doj.app.service.*)")
public void allMethodsInServicePackage();

Matching all methods defined in classes inside package com.doj.app.service and it’s sub-packages
sub-packages use two dots.

@Pointcut("within(com.doj.app.service..*)")
public void allMethodsInServicePackageAndSubPackages();

Matching all methods with a TransferService class

@Pointcut("within(com.doj.app.service.TransferService)")
public void allMethodsOfTransferService();

Matching all methods within all implementing classes of TransferService interface
Use + (plus) sign to match all implementations of an interface.

@Pointcut("within(com.doj.app.service.TransferService+)")
public void allMethodsOfTransferServiceImpl();

Matching all methods where the proxy implements the TransferService interface

@Pointcut("this(com.doj.app.service.TransferService)")
public void allMethodsProxyImplmentTransferService();

Matching all methods where the target object implements the TransferService interface

@Pointcut("target(com.doj.app.service.TransferService)")
public void allMethodsTargetObjectImplmentTransferService();

3. Matching Bean Name Patterns

Matching Bean Name Patterns is only supported in Spring AOP – and not in native AspectJ weaving.

Matching all methods on a Spring bean named transferService

@Pointcut("bean(transferService)")
public void allMethodsOfTransferServiceBean();

Matching all methods on Spring beans having names that match the wildcard expression *Service

@Pointcut("bean(*Service)")
public void allMethodsOfBeanNameAsTransferService();

4. Combining Pointcut Expressions

Pointcut expressions can be combined using ‘&&’, ‘||’ and ‘!’.

Matching any public methods within service module

@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}

@Pointcut("within(com.doj.app.service..*)")
private void inService() {}

@Pointcut("anyPublicOperation() && inService()")
private void serviceOperation() {}

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

3 years ago