Spring AOP

Spring AOP-Ordering of Aspects with Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In Spring AOP aspects ordering tutorial&comma; we will discuss how to use &commat;Order annotation and Order interface to decide precedence&sol;order of the aspects applied to the same join point&period; In my last tutorials of Spring AOP advice&comma; you have seen that we have applied single Aspect to a join point&period; In real time projects&comma; we have to use multiple aspects in our application and these aspects can be applied on a certain method&period; So in this situation there are more than one aspect applied to the same join point&period; In this case&comma; the order of execution of the aspects will not be determined unless we have explicitly specified by using either &commat;Order annotation or org&period;springframework&period;core&period;Ordered interface&period;<&sol;p>&NewLine;<h2 id&equals;"ads-id" align&equals;"center"> <b>Download Application Source Code<&sol;b><br &sol;>&NewLine;<b><a href&equals;"https&colon;&sol;&sol;github&period;com&sol;DOJ-SoftwareConsultant&sol;SpringAOP-Aspects-Order-Example" target&equals;"&lowbar;blank" rel&equals;"noopener">SpringAOP-Aspects-Order-Example from Github<&sol;a>&period;<&sol;b><&sol;h2>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon; 1px solid &num;c9e6f2&semi; border-radius&colon; 3px&semi; padding&colon; 16px&semi; line-height&colon; 1&period;45&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<p>Let&&num;8217&semi;s see Aspects Ordering with the example&period; In this example we have three aspects <b><i>LoggingAspect<&sol;i><&sol;b>&comma; <b><i>SecurityAspect <&sol;i><&sol;b>and <b><i>TransactionAspect <&sol;i><&sol;b>for same join point <b><i>transfer&lpar;&rpar;<&sol;i><&sol;b> method of <b><i>TransferService <&sol;i><&sol;b>class in the <b><i>com&period;doj&period;aopapp&period;service<&sol;i><&sol;b> package&period; Suppose we want to apply these aspects in the following order to the same join point &lpar;<b>transfer&lpar;&rpar; target method of TransferSerivce<&sol;b>&rpar;&period;<&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>SecurityAspect<&sol;li>&NewLine;<li>TransactionAspect<&sol;li>&NewLine;<li>LoggingAspect<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>So&comma; achieving this order of aspects we using two ways&period;<&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>&commat;Order Annoation<&sol;li>&NewLine;<li>org&period;springframework&period;core&period;Ordered interface<&sol;li>&NewLine;<&sol;ol>&NewLine;<h3><b>1&period; Specifying order by using &commat;Order annotation<&sol;b><&sol;h3>&NewLine;<p>This is very simple way&period; Use the annotation as below&period;<&sol;p>&NewLine;<p><b>SecurityAspect&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;aspect&semi; &NewLine; &NewLine;import org&period;aspectj&period;lang&period;JoinPoint&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Aspect&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Before&semi; &NewLine;import org&period;springframework&period;core&period;annotation&period;Order&semi; &NewLine;import org&period;springframework&period;stereotype&period;Component&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Aspect &NewLine;&commat;Component &NewLine;<b>&commat;Order&lpar;0&rpar;<&sol;b> &NewLine;public class SecurityAspect &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring before advice for all transfer methods whose taking three arguments of any type &NewLine; &ast; of all classes in the package com&period;doj&period;aopapp&period;service &NewLine; &ast; &commat;param jp &NewLine; &ast; &commat;throws Throwable &NewLine; &ast;&sol; &NewLine; &commat;Before&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;"&rpar; &NewLine; public void beforeAdviceForTransferMethods&lpar;JoinPoint jp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;SecurityAspect&period;beforeAdviceForTransferMethods&lpar;&rpar; " &plus; jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>TransactionAspect&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;aspect&semi; &NewLine; &NewLine;import org&period;aspectj&period;lang&period;JoinPoint&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Aspect&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Before&semi; &NewLine;import org&period;springframework&period;core&period;annotation&period;Order&semi; &NewLine;import org&period;springframework&period;stereotype&period;Component&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Aspect &NewLine;&commat;Component &NewLine;<b>&commat;Order&lpar;1&rpar;<&sol;b> &NewLine;public class TransactionAspect &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring before advice for all transfer methods whose taking three arguments of any type &NewLine; &ast; of all classes in the package com&period;doj&period;aopapp&period;service &NewLine; &ast; &commat;param jp &NewLine; &ast; &commat;throws Throwable &NewLine; &ast;&sol; &NewLine; &commat;Before&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;"&rpar; &NewLine; public void beforeAdviceForTransferMethods&lpar;JoinPoint jp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;TransactionAspect&period;beforeAdviceForTransferMethods&lpar;&rpar; " &plus; jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>LoggingAspect&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;aspect&semi; &NewLine; &NewLine;import org&period;aspectj&period;lang&period;ProceedingJoinPoint&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Around&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Aspect&semi; &NewLine;import org&period;springframework&period;core&period;annotation&period;Order&semi; &NewLine;import org&period;springframework&period;stereotype&period;Component&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Aspect &NewLine;&commat;Component &NewLine;<b>&commat;Order&lpar;2&rpar;<&sol;b> &NewLine;public class LoggingAspect &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring around advice for all transfer methods whose taking three arguments of any type &NewLine; &ast; of all classes in the package com&period;doj&period;aopapp&period;service &NewLine; &ast; &commat;param jp &NewLine; &ast; &commat;throws Throwable &NewLine; &ast;&sol; &NewLine; &commat;Around&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;"&rpar; &NewLine; public void beforeAdviceForTransferMethods&lpar;ProceedingJoinPoint pjp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;LoggingAspect Before execution of Transfer Methods " &plus; pjp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; pjp&period;proceed&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;LoggingAspect After execution of Transfer Methods " &plus; pjp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3><b>2&period; Specifying order by using <i>org&period;springframework&period;core&period;Ordered<&sol;i> interface<&sol;b><&sol;h3>&NewLine;<p>This is also very simple way&period; Use the interface as below&period;<&sol;p>&NewLine;<p><b>SecurityAspect&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;aspect&semi; &NewLine; &NewLine;import org&period;aspectj&period;lang&period;JoinPoint&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Aspect&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Before&semi; &NewLine;<b>import org&period;springframework&period;core&period;Ordered&semi;<&sol;b> &NewLine;import org&period;springframework&period;stereotype&period;Component&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Aspect &NewLine;&commat;Component &NewLine;public class SecurityAspect <b>implements Ordered<&sol;b>&lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring before advice for all transfer methods whose taking three arguments of any type &NewLine; &ast; of all classes in the package com&period;doj&period;aopapp&period;service &NewLine; &ast; &commat;param jp &NewLine; &ast; &commat;throws Throwable &NewLine; &ast;&sol; &NewLine; &commat;Before&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;"&rpar; &NewLine; public void beforeAdviceForTransferMethods&lpar;JoinPoint jp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;SecurityAspect&period;beforeAdviceForTransferMethods&lpar;&rpar; " &plus; jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; <b>&commat;Override &NewLine; public int getOrder&lpar;&rpar; &lbrace; &NewLine; return 0&semi; &NewLine; &rcub;<&sol;b> &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>TransactionAspect&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;aspect&semi; &NewLine; &NewLine;import org&period;aspectj&period;lang&period;JoinPoint&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Aspect&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Before&semi; &NewLine;<b>import org&period;springframework&period;core&period;Ordered&semi;<&sol;b> &NewLine;import org&period;springframework&period;stereotype&period;Component&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Aspect &NewLine;&commat;Component &NewLine;public class TransactionAspect <b>implements Ordered<&sol;b>&lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring before advice for all transfer methods whose taking three arguments of any type &NewLine; &ast; of all classes in the package com&period;doj&period;aopapp&period;service &NewLine; &ast; &commat;param jp &NewLine; &ast; &commat;throws Throwable &NewLine; &ast;&sol; &NewLine; &commat;Before&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;"&rpar; &NewLine; public void beforeAdviceForTransferMethods&lpar;JoinPoint jp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;TransactionAspect&period;beforeAdviceForTransferMethods&lpar;&rpar; " &plus; jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; <b>&commat;Override &NewLine; public int getOrder&lpar;&rpar; &lbrace; &NewLine; return 1&semi; &NewLine; &rcub;<&sol;b> &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>LoggingAspect&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;aspect&semi; &NewLine; &NewLine;import org&period;aspectj&period;lang&period;ProceedingJoinPoint&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Around&semi; &NewLine;import org&period;aspectj&period;lang&period;annotation&period;Aspect&semi; &NewLine;<b>import org&period;springframework&period;core&period;Ordered&semi;<&sol;b> &NewLine;import org&period;springframework&period;stereotype&period;Component&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Aspect &NewLine;&commat;Component &NewLine;public class LoggingAspect <b>implements Ordered<&sol;b> &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring around advice for all transfer methods whose taking three arguments of any type &NewLine; &ast; of all classes in the package com&period;doj&period;aopapp&period;service &NewLine; &ast; &commat;param jp &NewLine; &ast; &commat;throws Throwable &NewLine; &ast;&sol; &NewLine; &commat;Around&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;"&rpar; &NewLine; public void beforeAdviceForTransferMethods&lpar;ProceedingJoinPoint pjp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;LoggingAspect Before execution of Transfer Methods " &plus; pjp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; pjp&period;proceed&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;LoggingAspect After execution of Transfer Methods " &plus; pjp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; <b>&commat;Override &NewLine; public int getOrder&lpar;&rpar; &lbrace; &NewLine; return 2&semi; &NewLine; &rcub;<&sol;b> &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3><b>ApplicationContext Configuration file based on Java Config<&sol;b><br &sol;>&NewLine;<b><&sol;b><&sol;h3>&NewLine;<p><b>AppConfig&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;config&semi; &NewLine; &NewLine;import org&period;springframework&period;context&period;annotation&period;ComponentScan&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;Configuration&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;EnableAspectJAutoProxy&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Configuration &NewLine;&commat;EnableAspectJAutoProxy &NewLine;&commat;ComponentScan&lpar;basePackages&equals;&lbrace;"com&period;doj&period;aopapp&period;aspect"&comma; "com&period;doj&period;aopapp&period;service"&rcub;&rpar; &NewLine;public class AppConfig &lbrace; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3><b>ApplicationContext Configuration file based on XML Config<&sol;b><br &sol;>&NewLine;<b><&sol;b><&sol;h3>&NewLine;<p><b>applicationContext&period;xml<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8"&quest;&gt&semi; &NewLine;&lt&semi;beans xmlns&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans" &NewLine; xmlns&colon;xsi&equals;"http&colon;&sol;&sol;www&period;w3&period;org&sol;2001&sol;XMLSchema-instance" &NewLine; xmlns&colon;aop&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;aop" &NewLine; xmlns&colon;context&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context" &NewLine; xsi&colon;schemaLocation&equals;"http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;beans&sol;spring-beans&period;xsd &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;aop http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;aop&sol;spring-aop&period;xsd &NewLine; http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context http&colon;&sol;&sol;www&period;springframework&period;org&sol;schema&sol;context&sol;spring-context&period;xsd"&gt&semi; &NewLine; &NewLine; &lt&semi;aop&colon;config&gt&semi; &NewLine; &lt&semi;aop&colon;aspect ref&equals;"securityAspect" order&equals;"0"&gt&semi; &NewLine; &lt&semi;&excl;-- all public methods whose name are transfer&lpar;&rpar; with taking three arguments of any type and any return type of all classes in the com&period;doj&period;aopapp&period;service package --&gt&semi; &NewLine; &lt&semi;aop&colon;pointcut expression&equals;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;" id&equals;"logForAllTransfer"&sol;&gt&semi; &NewLine; &lt&semi;aop&colon;before method&equals;"beforeAdviceForTransferMethods" pointcut-ref&equals;"logForAllTransfer"&sol;&gt&semi; &NewLine; &lt&semi;&sol;aop&colon;aspect&gt&semi; &NewLine; &lt&semi;aop&colon;aspect ref&equals;"transactionAspect" order&equals;"1"&gt&semi; &NewLine; &lt&semi;&excl;-- all public methods whose name are transfer&lpar;&rpar; with taking three arguments of any type and any return type of all classes in the com&period;doj&period;aopapp&period;service package --&gt&semi; &NewLine; &lt&semi;aop&colon;pointcut expression&equals;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;" id&equals;"logForAllTransfer"&sol;&gt&semi; &NewLine; &lt&semi;aop&colon;before method&equals;"beforeAdviceForTransferMethods" pointcut-ref&equals;"logForAllTransfer"&sol;&gt&semi; &NewLine; &lt&semi;&sol;aop&colon;aspect&gt&semi; &NewLine; &lt&semi;aop&colon;aspect ref&equals;"loggingAspect" order&equals;"2"&gt&semi; &NewLine; &lt&semi;&excl;-- all public methods whose name are transfer&lpar;&rpar; with taking three arguments of any type and any return type of all classes in the com&period;doj&period;aopapp&period;service package --&gt&semi; &NewLine; &lt&semi;aop&colon;pointcut expression&equals;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;transfer&lpar;&ast;&comma;&ast;&comma;&ast;&rpar;&rpar;" id&equals;"logForAllTransfer"&sol;&gt&semi; &NewLine; &lt&semi;aop&colon;around method&equals;"beforeAdviceForTransferMethods" pointcut-ref&equals;"logForAllTransfer"&sol;&gt&semi; &NewLine; &lt&semi;&sol;aop&colon;aspect&gt&semi; &NewLine; &lt&semi;&sol;aop&colon;config&gt&semi; &NewLine; &NewLine; &lt&semi;bean id&equals;"transferService" class&equals;"com&period;doj&period;aopapp&period;service&period;TransferServiceImpl"&sol;&gt&semi; &NewLine; &lt&semi;bean id&equals;"securityAspect" class&equals;"com&period;doj&period;aopapp&period;aspect&period;SecurityAspect"&sol;&gt&semi; &NewLine;&lt&semi;bean id&equals;"transactionAspect" class&equals;"com&period;doj&period;aopapp&period;aspect&period;TransactionAspect"&sol;&gt&semi; &NewLine; &lt&semi;bean id&equals;"loggingAspect" class&equals;"com&period;doj&period;aopapp&period;aspect&period;LoggingAspect"&sol;&gt&semi; &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3><b>Test Class for Spring AspectJ Configuration and Execution<&sol;b><&sol;h3>&NewLine;<p>Let’s run the example as below&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;test&semi; &NewLine; &NewLine;import org&period;springframework&period;context&period;ConfigurableApplicationContext&semi; &NewLine;import org&period;springframework&period;context&period;annotation&period;AnnotationConfigApplicationContext&semi; &NewLine; &NewLine;import com&period;doj&period;aopapp&period;config&period;AppConfig&semi; &NewLine;import com&period;doj&period;aopapp&period;service&period;TransferService&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class Main &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine; ConfigurableApplicationContext applicationContext &equals; new AnnotationConfigApplicationContext&lpar;AppConfig&period;class&rpar;&semi; &NewLine; TransferService transferService &equals; applicationContext&period;getBean&lpar;TransferService&period;class&rpar;&semi; &NewLine; transferService&period;transfer&lpar;"accountA"&comma; "accountB"&comma; 50000l&rpar;&semi; &NewLine; applicationContext&period;close&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h2><b>Output on the Console&colon;<&sol;b><&sol;h2>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;">Mar 20&comma; 2017 5&colon;59&colon;16 PM org&period;springframework&period;context&period;annotation&period;AnnotationConfigApplicationContext prepareRefresh<br &sol;>&NewLine;INFO&colon; Refreshing org&period;springframework&period;context&period;annotation&period;AnnotationConfigApplicationContext&commat;6576fe71&colon; startup date &lbrack;Mon Mar 20 17&colon;59&colon;16 IST 2017&rsqb;&semi; root of context hierarchy<br &sol;>&NewLine;&ast;&ast;&ast;&ast;SecurityAspect&period;beforeAdviceForTransferMethods&lpar;&rpar; transfer<br &sol;>&NewLine;&ast;&ast;&ast;&ast;TransactionAspect&period;beforeAdviceForTransferMethods&lpar;&rpar; transfer<br &sol;>&NewLine;&ast;&ast;&ast;&ast;LoggingAspect Before execution of Transfer Methods transfer<br &sol;>&NewLine;50000 Amount has been tranfered from accountA to accountB<br &sol;>&NewLine;&ast;&ast;&ast;&ast;LoggingAspect After execution of Transfer Methods transfer<br &sol;>&NewLine;Mar 20&comma; 2017 5&colon;59&colon;17 PM org&period;springframework&period;context&period;annotation&period;AnnotationConfigApplicationContext doClose<br &sol;>&NewLine;INFO&colon; Closing org&period;springframework&period;context&period;annotation&period;AnnotationConfigApplicationContext&commat;6576fe71&colon; startup date &lbrack;Mon Mar 20 17&colon;59&colon;16 IST 2017&rsqb;&semi; root of context hierarchy<&sol;div>&NewLine;<p>As per as output on the console Spring AOP aspects ordering is working as expected&period;<&sol;p>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px&semi;"><span style&equals;"color&colon; red&semi; font-size&colon; x-large&semi; text-align&colon; center&semi;"><b>Spring AOP Related Posts<&sol;b><&sol;span><&sol;p>&NewLine;<ul>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-interview-questions-and-answers&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP Interview Questions and Answers<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-aop-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP-Introduction to Aspect Oriented Programming<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aspect-annotation&sol;"><span style&equals;"color&colon; red&semi;">&commat;Aspect Annotation in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;after-advice-type-and-around-advice&sol;"><span style&equals;"color&colon; red&semi;">Advices in Spring AOP <&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;joinpoints-and-advice-arguments-chapter&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP JoinPoints and Advice Arguments<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;pointcuts-and-wildcard-expressions&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP-Declaring pointcut Expressions with Examples<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;aop-xml-configuration-chapter-30&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP XML configuration<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;xml-schema-based-aop-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP XML Schema based Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-before-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;Before Annotation Advice Example<br &sol;>&NewLine;<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-before-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP Before Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-after-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;After Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-after-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP After Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-after-returning-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;AfterReturning Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-after-returning-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP After-Returning Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-after-throwing-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;AfterThrowing Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-after-throwing-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP After Throwing Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-aspectj-around-annotation-advice-example&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP AspectJ &commat;Around Annotation Advice Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-around-advice-example-using-xml-config&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP Around Advice Example using XML Config<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;writing-first-aspectj-program-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP Writing First AspectJ Program in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;understanding-aop-proxies-chapter-31&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP Proxies in Spring<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-transaction-management-in&sol;"><span style&equals;"color&colon; red&semi;"> Spring AOP Transaction Management in Hibernate<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;transaction-management-in-spring&sol;"><span style&equals;"color&colon; red&semi;">Spring Transaction Management<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;declarative-transaction-management&sol;"><span style&equals;"color&colon; red&semi;">Spring Declarative Transaction Management Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-ordering-of-aspects-with-example&sol;"><span style&equals;"color&colon; red&semi;">Spring AOP-Ordering of Aspects with Example<&sol;span><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-around-advice-example-using-xml-config&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '44'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago