<div dir="ltr" style="text-align: justify;">
<p>In Spring AOP aspects ordering tutorial, we will discuss how to use @Order annotation and Order interface to decide precedence/order of the aspects applied to the same join point. In my last tutorials of Spring AOP advice, you have seen that we have applied single Aspect to a join point. In real time projects, we have to use multiple aspects in our application and these aspects can be applied on a certain method. So in this situation there are more than one aspect applied to the same join point. In this case, the order of execution of the aspects will not be determined unless we have explicitly specified by using either @Order annotation or org.springframework.core.Ordered interface.</p>
<h2 id="ads-id" align="center"> <b>Download Application Source Code</b><br />
<b><a href="https://github.com/DOJ-SoftwareConsultant/SpringAOP-Aspects-Order-Example" target="_blank" rel="noopener">SpringAOP-Aspects-Order-Example from Github</a>.</b></h2>
<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>Let&#8217;s see Aspects Ordering with the example. In this example we have three aspects <b><i>LoggingAspect</i></b>, <b><i>SecurityAspect </i></b>and <b><i>TransactionAspect </i></b>for same join point <b><i>transfer()</i></b> method of <b><i>TransferService </i></b>class in the <b><i>com.doj.aopapp.service</i></b> package. Suppose we want to apply these aspects in the following order to the same join point (<b>transfer() target method of TransferSerivce</b>).</p>
<ol style="text-align: left;">
<li>SecurityAspect</li>
<li>TransactionAspect</li>
<li>LoggingAspect</li>
</ol>
<p>So, achieving this order of aspects we using two ways.</p>
<ol style="text-align: left;">
<li>@Order Annoation</li>
<li>org.springframework.core.Ordered interface</li>
</ol>
<h3><b>1. Specifying order by using @Order annotation</b></h3>
<p>This is very simple way. Use the annotation as below.</p>
<p><b>SecurityAspect.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.springframework.core.annotation.Order; 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Aspect 
@Component 
<b>@Order(0)</b> 
public class SecurityAspect { 
 
 /** 
 * Declaring before advice for all transfer methods whose taking three arguments of any type 
 * of all classes in the package com.doj.aopapp.service 
 * @param jp 
 * @throws Throwable 
 */ 
 @Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
 public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable { 
 System.out.println("****SecurityAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName()); 
 } 
} 
</pre>
<p><b>TransactionAspect.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
import org.springframework.core.annotation.Order; 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Aspect 
@Component 
<b>@Order(1)</b> 
public class TransactionAspect { 
 
 /** 
 * Declaring before advice for all transfer methods whose taking three arguments of any type 
 * of all classes in the package com.doj.aopapp.service 
 * @param jp 
 * @throws Throwable 
 */ 
 @Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
 public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable { 
 System.out.println("****TransactionAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName()); 
 } 
 
} 
 
</pre>
<p><b>LoggingAspect.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
import org.springframework.core.annotation.Order; 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Aspect 
@Component 
<b>@Order(2)</b> 
public class LoggingAspect { 
 
 /** 
 * Declaring around advice for all transfer methods whose taking three arguments of any type 
 * of all classes in the package com.doj.aopapp.service 
 * @param jp 
 * @throws Throwable 
 */ 
 @Around("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
 public void beforeAdviceForTransferMethods(ProceedingJoinPoint pjp) throws Throwable { 
 System.out.println("****LoggingAspect Before execution of Transfer Methods " + pjp.getSignature().getName()); 
 pjp.proceed(); 
 System.out.println("****LoggingAspect After execution of Transfer Methods " + pjp.getSignature().getName()); 
 } 
} 
 
</pre>
<h3><b>2. Specifying order by using <i>org.springframework.core.Ordered</i> interface</b></h3>
<p>This is also very simple way. Use the interface as below.</p>
<p><b>SecurityAspect.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
<b>import org.springframework.core.Ordered;</b> 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Aspect 
@Component 
public class SecurityAspect <b>implements Ordered</b>{ 
 
 /** 
 * Declaring before advice for all transfer methods whose taking three arguments of any type 
 * of all classes in the package com.doj.aopapp.service 
 * @param jp 
 * @throws Throwable 
 */ 
 @Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
 public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable { 
 System.out.println("****SecurityAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName()); 
 } 
 
 <b>@Override 
 public int getOrder() { 
 return 0; 
 }</b> 
} 
 
</pre>
<p><b>TransactionAspect.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.JoinPoint; 
import org.aspectj.lang.annotation.Aspect; 
import org.aspectj.lang.annotation.Before; 
<b>import org.springframework.core.Ordered;</b> 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Aspect 
@Component 
public class TransactionAspect <b>implements Ordered</b>{ 
 
 /** 
 * Declaring before advice for all transfer methods whose taking three arguments of any type 
 * of all classes in the package com.doj.aopapp.service 
 * @param jp 
 * @throws Throwable 
 */ 
 @Before("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
 public void beforeAdviceForTransferMethods(JoinPoint jp) throws Throwable { 
 System.out.println("****TransactionAspect.beforeAdviceForTransferMethods() " + jp.getSignature().getName()); 
 } 
 
 <b>@Override 
 public int getOrder() { 
 return 1; 
 }</b> 
 
} 
 
</pre>
<p><b>LoggingAspect.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.aspect; 
 
import org.aspectj.lang.ProceedingJoinPoint; 
import org.aspectj.lang.annotation.Around; 
import org.aspectj.lang.annotation.Aspect; 
<b>import org.springframework.core.Ordered;</b> 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Aspect 
@Component 
public class LoggingAspect <b>implements Ordered</b> { 
 
 /** 
 * Declaring around advice for all transfer methods whose taking three arguments of any type 
 * of all classes in the package com.doj.aopapp.service 
 * @param jp 
 * @throws Throwable 
 */ 
 @Around("execution(* com.doj.aopapp.service.*.transfer(*,*,*))") 
 public void beforeAdviceForTransferMethods(ProceedingJoinPoint pjp) throws Throwable { 
 System.out.println("****LoggingAspect Before execution of Transfer Methods " + pjp.getSignature().getName()); 
 pjp.proceed(); 
 System.out.println("****LoggingAspect After execution of Transfer Methods " + pjp.getSignature().getName()); 
 } 
 
 <b>@Override 
 public int getOrder() { 
 return 2; 
 }</b> 
} 
 
</pre>
<h3><b>ApplicationContext Configuration file based on Java Config</b><br />
<b></b></h3>
<p><b>AppConfig.java</b></p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.config; 
 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.EnableAspectJAutoProxy; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
@Configuration 
@EnableAspectJAutoProxy 
@ComponentScan(basePackages={"com.doj.aopapp.aspect", "com.doj.aopapp.service"}) 
public class AppConfig { 
 
} 
 
</pre>
<h3><b>ApplicationContext Configuration file based on XML Config</b><br />
<b></b></h3>
<p><b>applicationContext.xml</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:aop="http://www.springframework.org/schema/aop" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">; 
 
 <;aop:config>; 
 <;aop:aspect ref="securityAspect" order="0">; 
 <;!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package -->; 
 <;aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>; 
 <;aop:before method="beforeAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/>; 
 <;/aop:aspect>; 
 <;aop:aspect ref="transactionAspect" order="1">; 
 <;!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package -->; 
 <;aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>; 
 <;aop:before method="beforeAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/>; 
 <;/aop:aspect>; 
 <;aop:aspect ref="loggingAspect" order="2">; 
 <;!-- all public methods whose name are transfer() with taking three arguments of any type and any return type of all classes in the com.doj.aopapp.service package -->; 
 <;aop:pointcut expression="execution(* com.doj.aopapp.service.*.transfer(*,*,*))" id="logForAllTransfer"/>; 
 <;aop:around method="beforeAdviceForTransferMethods" pointcut-ref="logForAllTransfer"/>; 
 <;/aop:aspect>; 
 <;/aop:config>; 
 
 <;bean id="transferService" class="com.doj.aopapp.service.TransferServiceImpl"/>; 
 <;bean id="securityAspect" class="com.doj.aopapp.aspect.SecurityAspect"/>; 
<;bean id="transactionAspect" class="com.doj.aopapp.aspect.TransactionAspect"/>; 
 <;bean id="loggingAspect" class="com.doj.aopapp.aspect.LoggingAspect"/>; 
<;/beans>; 
 
</pre>
<h3><b>Test Class for Spring AspectJ Configuration and Execution</b></h3>
<p>Let’s run the example as below:</p>
<pre class="highlight">/** 
 * 
 */ 
package com.doj.aopapp.test; 
 
import org.springframework.context.ConfigurableApplicationContext; 
import org.springframework.context.annotation.AnnotationConfigApplicationContext; 
 
import com.doj.aopapp.config.AppConfig; 
import com.doj.aopapp.service.TransferService; 
 
/** 
 * @author Dinesh.Rajput 
 * 
 */ 
public class Main { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); 
 TransferService transferService = applicationContext.getBean(TransferService.class); 
 transferService.transfer("accountA", "accountB", 50000l); 
 applicationContext.close(); 
 } 
 
} 
 
</pre>
<h2><b>Output on the Console:</b></h2>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">Mar 20, 2017 5:59:16 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh<br />
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@6576fe71: startup date [Mon Mar 20 17:59:16 IST 2017]; root of context hierarchy<br />
****SecurityAspect.beforeAdviceForTransferMethods() transfer<br />
****TransactionAspect.beforeAdviceForTransferMethods() transfer<br />
****LoggingAspect Before execution of Transfer Methods transfer<br />
50000 Amount has been tranfered from accountA to accountB<br />
****LoggingAspect After execution of Transfer Methods transfer<br />
Mar 20, 2017 5:59:17 PM org.springframework.context.annotation.AnnotationConfigApplicationContext doClose<br />
INFO: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6576fe71: startup date [Mon Mar 20 17:59:16 IST 2017]; root of context hierarchy</div>
<p>As per as output on the console Spring AOP aspects ordering is working as expected.</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>
<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>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-aop-around-advice-example-using-xml-config/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/">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: '44'});
});
</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…