<div dir="ltr" style="text-align: justify;">
<p>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:</p>
<ul>
<li>Matching Method Signature Patterns</li>
<li>Matching Type Signature Patterns</li>
<li>Matching Bean Name Patterns</li>
<li>Combining Pointcut Expressions</li>
</ul>
<h2><b>Supported Pointcut Designators by Spring AOP</b></h2>
<p>AspectJ framework supports many Designators but Spring AOP supports only some of them as below:</p>
<ul style="text-align: left;">
<li><b>execution </b>&#8211; pointcut expression for matching method execution join points.</li>
<li><b>within </b>&#8211; pointcut expression for matching to join points within certain types</li>
<li><b>this </b>&#8211; pointcut expression for matching to join points where the bean reference is an instance of the given type</li>
<li><b>target </b>&#8211; pointcut expression for matching to join points where the target object is an instance of the given type</li>
<li><b>args </b>&#8211; pointcut expression for matching to join points where the arguments are instances of the given types</li>
<li><b>@target</b> &#8211; pointcut expression for matching to join points where the class of the executing object has an annotation of the given type</li>
<li><b>@args</b> &#8211; pointcut expression for matching to join points where the runtime type of the actual arguments passed have annotations of the given type</li>
<li><b>@within</b> &#8211; pointcut expression for matching to join points within types that have the given annotation</li>
<li><b>@annotation</b> &#8211; pointcut expression for matching to join points where the subject of the join point has the given annotation</li>
</ul>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<h2><b><img class="aligncenter wp-image-3185 size-full" src="https://dineshonjava.com/wp-content/uploads/2012/11/pointcuts-expression.png" alt="Declaring pointcut Expressions" width="748" height="247" />Declaring pointcut Expressions</b></h2>
<h3><b>1. Matching Method Signature Patterns</b></h3>
<p><b>Matching all public methods in TransferService</b><br />
Use public keyword in start, use * to match any return type.</p>
<pre class="highlight">@Pointcut("execution(public * com.doj.app.service.TransferService.*(..))") 
public void anyPublicMethodOfTrasferService(); 
</pre>
<p><b>Matching any public methods</b><br />
Use public keyword in start, use * to match any return type and use another * to match any method name.</p>
<pre class="highlight">@Pointcut("execution(public * *(..))") 
public void anyPublicMethod(); 
</pre>
<p><b>Matching any method defined in the service package</b><br />
Use * in start to match any return type, use second * to match any class name and use another * to match any method name.</p>
<pre class="highlight">@Pointcut("execution(* com.doj.app.service.*.*(..))") 
public void anyMethodInServicePackage(); 
</pre>
<p><b>Matching any method defined in the service package or it&#8217;s sub-packages</b><br />
Use * in start to match any return type, use two dots after service package means it&#8217;s include sub-packages as well, use second * to match any class name and use another * to match any method name.</p>
<pre class="highlight">@Pointcut("execution(* com.doj.app.service..*.*(..))") 
public void anyMethodInServicePackageAndSubPackage(); 
</pre>
<p><b>Matching all public methods in TransferService with return type Account</b><br />
Use public keyword in start and use Account as a return type.</p>
<pre class="highlight">@Pointcut("execution(public Account com.doj.app.service.TransferService.*(..))") 
public void allPublicMethodOfTransferServiceReturnTypeAccount(); 
</pre>
<p><b>Matching all methods in TransferService with return type void and first parameter as Account</b><br />
Use void keyword in start and use Account as a argument type for first parameter.</p>
<pre class="highlight">@Pointcut("execution(void com.doj.app.service.TransferService.*(Account account, ..))") 
public void allMethodOfTransferServiceVoidReturnTypeFirstArgumentAccount(); 
</pre>
<p><b>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</b><br />
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.</p>
<pre class="highlight">@Pointcut("execution(public * com.doj.app.service.*.tranfer(Account account1, Account account2))") 
public void allTranferMethodsInServicePackageWithTwoArgumentsOfAccountType(); 
</pre>
<h3><b>2. Matching Type Signature Patterns</b></h3>
<p>Method execution only in Spring AOP. It provides narrowed to matching all method executions within the certain types only.</p>
<p><b>Matching all methods defined in classes inside package <i>com.doj.app.service</i></b></p>
<pre class="highlight">@Pointcut("within(com.doj.app.service.*)") 
public void allMethodsInServicePackage(); 
</pre>
<p><b>Matching all methods defined in classes inside package <i>com.doj.app.service</i> and it&#8217;s sub-packages</b><br />
<b>sub-packages use two dots.</b></p>
<pre class="highlight">@Pointcut("within(com.doj.app.service..*)") 
public void allMethodsInServicePackageAndSubPackages(); 
</pre>
<p><b>Matching all methods with a TransferService class</b></p>
<pre class="highlight">@Pointcut("within(com.doj.app.service.TransferService)") 
public void allMethodsOfTransferService(); 
</pre>
<p><b>Matching all methods within all implementing classes of TransferService interface</b><br />
Use + (plus) sign to match all implementations of an interface.</p>
<pre class="highlight">@Pointcut("within(com.doj.app.service.TransferService+)") 
public void allMethodsOfTransferServiceImpl(); 
</pre>
<p><b>Matching all methods where the proxy implements the TransferService interface</b></p>
<pre class="highlight">@Pointcut("this(com.doj.app.service.TransferService)") 
public void allMethodsProxyImplmentTransferService(); 
</pre>
<p><b>Matching all methods where the target object implements the TransferService interface</b></p>
<pre class="highlight">@Pointcut("target(com.doj.app.service.TransferService)") 
public void allMethodsTargetObjectImplmentTransferService(); 
</pre>
<h3><b>3. Matching Bean Name Patterns</b></h3>
<p>Matching Bean Name Patterns is only supported in Spring AOP &#8211; and not in native AspectJ weaving.</p>
<p><b>Matching all methods on a Spring bean named <i>transferService</i></b></p>
<pre class="highlight">@Pointcut("bean(transferService)") 
public void allMethodsOfTransferServiceBean(); 
</pre>
<p><b>Matching all methods on Spring beans having names that match the wildcard expression <i>*Service</i></b></p>
<pre class="highlight">@Pointcut("bean(*Service)") 
public void allMethodsOfBeanNameAsTransferService(); 
</pre>
<h3><b>4. Combining Pointcut Expressions</b></h3>
<p>Pointcut expressions can be combined using &#8216;&;&;&#8217;, &#8216;||&#8217; and &#8216;!&#8217;.</p>
<p><b>Matching any public methods within service module</b></p>
<pre class="highlight">@Pointcut("execution(public * *(..))") 
private void anyPublicOperation() {} 
 
@Pointcut("within(com.doj.app.service..*)") 
private void inService() {} 
 
@Pointcut("anyPublicOperation() &;&; inService()") 
private void serviceOperation() {} 
</pre>
<p> ;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Spring AOP Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-aop-interview-questions-and-answers/"><span style="color: red;">Spring AOP Interview Questions and Answers</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-aop-in-spring/"><span style="color: red;">Spring AOP-Introduction to Aspect Oriented Programming</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aspect-annotation/"><span style="color: red;">@Aspect Annotation in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/after-advice-type-and-around-advice/"><span style="color: red;">Advices in Spring AOP </span></a></b></li>
<li><b><a href="https://dineshonjava.com/joinpoints-and-advice-arguments-chapter/"><span style="color: red;">Spring AOP JoinPoints and Advice Arguments</span></a></b></li>
<li><b><a href="https://dineshonjava.com/pointcuts-and-wildcard-expressions/"><span style="color: red;">Spring AOP-Declaring pointcut Expressions with Examples</span></a></b></li>
<li><b><a href="https://dineshonjava.com/aop-xml-configuration-chapter-30/"><span style="color: red;">Spring AOP XML configuration</span></a></b></li>
<li><b><a href="https://dineshonjava.com/xml-schema-based-aop-example/"><span style="color: red;"> Spring AOP XML Schema based Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-before-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Before Annotation Advice Example<br />
</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-before-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Before Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @After Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-returning-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterReturning Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-returning-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After-Returning Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-after-throwing-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @AfterThrowing Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-after-throwing-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP After Throwing Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-aspectj-around-annotation-advice-example/"><span style="color: red;"> Spring AOP AspectJ @Around Annotation Advice Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/"><span style="color: red;"> Spring AOP Around Advice Example using XML Config</span></a></b></li>
<li><b><a href="https://dineshonjava.com/writing-first-aspectj-program-in-spring/"><span style="color: red;">Spring AOP Writing First AspectJ Program in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-aop-proxies-chapter-31/"><span style="color: red;">Spring AOP Proxies in Spring</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-transaction-management-in/"><span style="color: red;"> Spring AOP Transaction Management in Hibernate</span></a></b></li>
<li><b><a href="https://dineshonjava.com/transaction-management-in-spring/"><span style="color: red;">Spring Transaction Management</span></a></b></li>
<li><b><a href="https://dineshonjava.com/declarative-transaction-management/"><span style="color: red;">Spring Declarative Transaction Management Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-ordering-of-aspects-with-example/"><span style="color: red;">Spring AOP-Ordering of Aspects with Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/interview-questions-on-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/joinpoints-and-advice-arguments-chapter/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '642'});
});
</script>
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…