<div dir="ltr" style="text-align: justify;" trbidi="on"><b>@AspectJ</b> refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations. The @AspectJ style was introduced by the AspectJ project as part of the AspectJ 5 release. Spring 2.0 interprets the same annotations as AspectJ 5, using a library supplied by AspectJ for pointcut parsing and matching. The AOP runtime is still pure Spring AOP though, and there is no dependency on the AspectJ compiler or weaver.</p>
<div align="center" id="ads-id"></div>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;">
<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>
<p>The @AspectJ support is enabled by including the following element inside your spring configuration: </p>
<pre class="highlight"><;aop:aspectj-autoproxy/>;</pre>
<p>You also need to add following libraries on class path</p>
<ul class="list">
<li><b>aspectjrt.jar</b></li>
<li><b>aspectjweaver.jar</b></li>
<li><b>aspectj.jar</b></li>
</ul>
<p><b><br />
Declaring an aspect</b><br />
With the @AspectJ support enabled, any bean defined in your application context with a class that is an @AspectJ aspect (has the @Aspect annotation) will be automatically detected by Spring and used to configure Spring AOP. The following example shows the minimal definition required for a logging aspect:</p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.aop.aspect; 
import org.aspectj.lang.annotation.Aspect; 
@Aspect 
public class LoggingAspect 
{ 
 //.... 
} 
</pre>
<p>They will be configured in XML like any other bean as follows:</p>
<pre class="highlight" name="code"><;bean class="com.dineshonjava.sdnext.aop.aspect.LoggingAspect" id="logAspect">; 
 <;!-- configure properties of aspect here as normal -->; 
<;/bean>; 
</pre>
<p><b><br />
Declaring a pointcut</b><br />
Spring AOP only supports method execution join points for Spring beans, so you can think of a pointcut as matching the execution of methods on Spring beans.<br />
A <b>pointcut</b> helps in determining the join points (ie methods) of interest to be executed with different advices. While working with @AspectJ based configuration, pointcut declaration has two parts:</p>
<ul class="list">
<li>A pointcut expression that determines exactly which method executions we are interested in.</li>
<li>A pointcut signature comprising a name and any number of parameters. The actual body of the method is irrelevant and in fact should be empty.</li>
</ul>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.aop.aspect; 
 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
@Aspect 
public class LoggingAspect 
{ 
 @Pointcut("execution(* com.dineshonjava.sdnext.aop.emp.Employee.addEmployee(..))") 
 public void addEmployee(){} 
} 
</pre>
<p>The following example defines a pointcut named &#8216;businessService&#8217; that will match the execution of every method available in the classes under the package com.dineshonjava.sdnext.aop.aspect.service:</p>
<pre class="highlight" name="code">import org.aspectj.lang.annotation.Pointcut; 
 
@Pointcut("execution(* com.dineshonjava.sdnext.aop.aspect.service.*.*(..))") // expression 
private void businessService() {} // signature 
</pre>
<h2>Combining pointcut expressions</h2>
<p>Pointcut expressions can be combined using &#8216;&;&;&#8217;, &#8216;||&#8217; and &#8216;!&#8217;. It is also possible to refer to pointcut expressions by name. The following example shows three pointcut expressions: anyPublicOperation (which matches if a method execution join point represents the execution of any public method); inTrading (which matches if a method execution is in the trading module), and tradingOperation (which matches if a method execution represents any public method in the trading module).</p>
<pre class="highlight" name="code">@Pointcut("execution(public * *(..))") 
 private void anyPublicOperation() {} 
 
 @Pointcut("within(com.dineshonjava.sdnext.aop.aspect.trading..*)") 
 private void inTrading() {} 
 
 @Pointcut("anyPublicOperation() &;&; inTrading()") 
 private void tradingOperation() {} 
</pre>
<h2>Sharing common pointcut definitions</h2>
<p>When working with enterprise applications, you often want to refer to modules of the application and particular sets of operations from within several aspects. We recommend defining a &#8220;SystemArchitecture&#8221; aspect that captures common pointcut expressions for this purpose. A typical such aspect would look as follows:</p>
<pre class="highlight" name="code">package com.dineshonjava.sdnext.aop.aspect; 
 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Pointcut; 
 
@Aspect 
public class SystemArchitecture { 
 
 /** 
 * A join point is in the web layer if the method is defined 
 * in a type in the com.dineshonjava.sdnext.aop.aspect.web package or any sub-package 
 * under that. 
 */ 
 @Pointcut("within(com.dineshonjava.sdnext.aop.aspect.web..*)") 
 public void inWebLayer() {} 
 
 /** 
 * A join point is in the service layer if the method is defined 
 * in a type in the com.dineshonjava.sdnext.aop.aspect.service package or any sub-package 
 * under that. 
 */ 
 @Pointcut("within(com.dineshonjava.sdnext.aop.aspect.service..*)") 
 public void inServiceLayer() {} 
 
 /** 
 * A join point is in the data access layer if the method is defined 
 * in a type in the com.dineshonjava.sdnext.aop.aspect.dao package or any sub-package 
 * under that. 
 */ 
 @Pointcut("within(com.dineshonjava.sdnext.aop.aspect.dao..*)") 
 public void inDataAccessLayer() {} 
 
 /** 
 * A business service is the execution of any method defined on a service 
 * interface. This definition assumes that interfaces are placed in the 
 * "service" package, and that implementation types are in sub-packages. 
 * 
 * If you group service interfaces by functional area (for example, 
 * in packages com.dineshonjava.sdnext.aop.aspect.abc.service and com.dineshonjava.sdnext.aop.aspect.def.service) then 
 * the pointcut expression "execution(* com.dineshonjava.sdnext.aop.aspect..service.*.*(..))" 
 * could be used instead. 
 * 
 * Alternatively, you can write the expression using the 'bean' 
 * PCD, like so "bean(*Service)". (This assumes that you have 
 * named your Spring service beans in a consistent fashion.) 
 */ 
 @Pointcut("execution(* com.dineshonjava.sdnext.aop.aspect.service.*.*(..))") 
 public void businessService() {} 
 
 /** 
 * A data access operation is the execution of any method defined on a 
 * dao interface. This definition assumes that interfaces are placed in the 
 * "dao" package, and that implementation types are in sub-packages. 
 */ 
 @Pointcut("execution(* com.dineshonjava.sdnext.aop.aspect.dao.*.*(..))") 
 public void dataAccessOperation() {} 
 
} 
</pre>
<p>The pointcuts defined in such an aspect can be referred to anywhere that you need a pointcut expression. For example, to make the service layer transactional, you could write:</p>
<pre class="highlight" name="code"><;aop:config>; 
 <;aop:advisor advice-ref="tx-advice" pointcut="com.dineshonjava.sdnext.aop.aspect.SystemArchitecture.businessService()">; 
<;/aop:advisor>;<;/aop:config>; 
 
<;tx:advice id="tx-advice">; 
 <;tx:attributes>; 
 <;tx:method name="*" propagation="REQUIRED">; 
 <;/tx:method>;<;/tx:attributes>; 
<;/tx:advice>; 
</pre>
<p><b><br />
Declaring advices</b><br />
You can declare any of the five advices using @{ADVICE-NAME} annotations as given below.</p>
<ol>
<li><b>@Before</b> – Run before the method execution</li>
<li><b>@After</b> – Run after the method returned a result</li>
<li><b>@AfterReturning</b> – Run after the method returned a result, intercept the returned result as well.</li>
<li><b>@AfterThrowing</b> – Run after the method throws an exception</li>
<li><b>@Around</b> – Run around the method execution, combine all three advices above.</li>
</ol>
<pre class="highlight" name="code">@Before("businessService()") 
public void doBeforeTask(){ 
 ... 
} 
 
@After("businessService()") 
public void doAfterTask(){ 
 ... 
} 
 
@AfterReturning(pointcut = "businessService()", returning="retVal") 
public void doAfterReturnningTask(Object retVal){ 
 // you can intercept retVal here. 
 ... 
} 
 
@AfterThrowing(pointcut = "businessService()", throwing="ex") 
public void doAfterThrowingTask(Exception ex){ 
 // you can intercept thrown exception here. 
 ... 
} 
 
@Around("businessService()") 
public void doAroundTask(){ 
 ... 
} 
</pre>
<p>
You can define you pointcut inline for any of the advices. Below is an example to define inline pointcut for before advice:</p>
<pre class="highlight" name="code">@Before("execution(* com.dineshonjava.sdnext.aop.aspect.service.*.*(..))") 
public doBeforeTask(){ 
 ... 
} 
</pre>
<p>
<;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<span style="color: red; font-size: x-large; text-align: center;"><b>Spring AOP Related Posts</b></span></p>
<ol style="text-align: left;"><span style="color: #274e13;"></p>
<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>
<p></span></ol>
</div>
<p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-jsr-250-annotations/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/stereotype-annotations-in-spring/">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: '654'});
});
</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…