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 ExpressionsDeclaring 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() {}

 

Spring AOP Related Posts

 

Previous
Next