Spring AOP + AspectJ @Pointcut Annotation Example
In this tutorial, we show you how to use @Pointcut
annotation with Spring AOP and AspectJ. A pointcut is a condition which matches to the join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut and join point is a point during the execution of a program.
Declaring a Pointcut
Spring provides the @Pointcut
annotation to declare a pointcut and we can use the expression to define it.
@Pointcut("execution(* org.websparrow.service.Bank.*.(..))")// the pointcut expression
private void anyOldTransfer() {}// the pointcut signature
Let’s see some common examples of pointcut expressions are given below.
the execution of any public method:
execution(public * *(..))
the execution of any method with a name beginning with set
:
execution(* set*(..))
the execution of any method defined by the Bank
interface/class:
execution(* org.websparrow.service.Bank.*(..))
the execution of any method defined in the service package:
execution(* org.websparrow.service.*.*(..))
the execution of any method defined in the service package or a sub-package:
execution(* org.websparrow.service..*.*(..))
any join point (method execution only in Spring AOP) within the service package:
within(org.websparrow.service.*)
any join point (method execution only in Spring AOP) within the service package or a sub-package:
within(org.websparrow.service..*)
In the previous tutorial, you have seen Bank
class has only one method i.e. deposit()
add one more method checkBalance()
and run the code. You will notice that the logging service will be executed for both methods but you want to do the logging only for deposit method, how can you do that. To solve this problem @Pointcut
comes into the role.
package org.websparrow.service;
import org.springframework.stereotype.Service;
@Service
public class Bank {
public String deposit(String accountNumber) {
System.out.println("inside deposit()");
if (accountNumber.equals("YES123")) {
System.out.println("You have successfully deposited your amount to the respective account number.");
return "success";
} else {
throw new InvalidAccountNo();
}
}
public void checkBalance(String accountNumber) {
System.out.println("inside checkBalance()");
if (accountNumber.equals("YES123")) {
System.out.println("Total amount in your account YES123 is Rs. 153000");
} else {
throw new InvalidAccountNo();
}
}
}
Declare the pointcut for a specific method with help of expression and call it where you want it.
package org.websparrow.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class LogBeforeAdvice {
// It will execute only for Bank's deposit method
@Pointcut("execution(* org.websparrow.service.Bank.deposit(..))")
public void depositPointcut() {
}
@Before("depositPointcut()")
public void logBefore() {
System.out.println(".............I WILL EXECUTE ONLY BEFORE DEPOSIT METHOD.............");
}
}
Now call both methods in Client
class and you will see that log service only executed for deposit()
method.
.............I WILL EXECUTE ONLY BEFORE DEPOSIT METHOD.............
inside deposit()
You have successfully deposited your amount to the respective account number.
inside checkBalance()
Total amount in your account YES123 is Rs. 153000
Download Source Code: spring-aop-aspectj-pointcut-annotation-example