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:
AspectJ framework supports many Designators but Spring AOP supports only some of them as below:
Popular Tutorials
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();
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();
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();
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
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…