Spring AOP

Spring AOP AspectJ @Around Annotation Advice Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In this spring aop Around advice example&comma; we will learn how to use aspectj &commat;Around annotation with java configuration&period; In Spring AOP&comma; Advice that surrounds a join point such as a method invocation&period; This is the most powerful kind of advice&period; Around advice can perform custom behavior before and after the method invocation&period; It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception i&period;e a method which annotated with AspectJ &commat;Around annotation run before and after any matching pointcut expression&comma; advised method execution&period;<&sol;p>&NewLine;<h2 id&equals;"ads-id" align&equals;"center"> <b>Download Application Source Code<&sol;b><&sol;h2>&NewLine;<h3><b><a href&equals;"https&colon;&sol;&sol;github&period;com&sol;DOJ-SoftwareConsultant&sol;SpringAOP-Around-Advice-Annotation" target&equals;"&lowbar;blank" rel&equals;"noopener">Spring AOP AspectJ &commat;Around Annotation Advice Example from GitHub<&sol;a>&period;<&sol;b><&sol;h3>&NewLine;<p>Let&&num;8217&semi;s create a simple spring application and add logging aspect to be invoked every joint point in the service class in the application&period; This example is also available with XML configuration in the application <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;2017&sol;03&sol;spring-aop-around-advice-example-using-xml-config&period;html"><b>Spring AOP Around Advice Example<&sol;b><&sol;a>&period;<&sol;p>&NewLine;<h2><b>AspectJ &commat;Around Annotation<&sol;b><&sol;h2>&NewLine;<p>&commat;Around annotation is an Aspectj annotation&comma; it is not Spring AOP annotation&comma; so we have to add Aspectj maven dependency with Spring AOP in this example&period; Let&&num;8217&semi;s see our LoggingAspect class with Around advice annotation&period;<&sol;p>&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>&nbsp&semi;<&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;aspectj&period;lang&period;annotation&period;Pointcut&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;public class LoggingAspect &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring around advice &NewLine; &ast; &commat;param jp &NewLine; &ast; &commat;throws Throwable &NewLine; &ast;&sol; &NewLine; &sol;&sol;&commat;Around&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;&ast;&lpar;&period;&period;&rpar;&rpar;"&rpar; &sol;&sol; around advice with pointcut expression directly &NewLine; &commat;Around&lpar;"logForAllMethods&lpar;&rpar;"&rpar; &sol;&sol;around advice with name pointcut that declared as name logForAllMethods&lpar;&rpar; &NewLine; public void aroundAdviceForAllMethods&lpar;ProceedingJoinPoint jp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;Before advised method execution "&plus;jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&plus;" LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;"&rpar;&semi; &NewLine; jp&period;proceed&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;After advised method execution "&plus;jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&plus;" LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;" &rpar;&semi; &NewLine; System&period;out&period;println&lpar;&rpar;&semi; &NewLine; &rcub; &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 aroundAdviceForTransferMethods&lpar;ProceedingJoinPoint jp&rpar; throws Throwable &lbrace; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;Before advised method execution "&plus;jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&plus;" LoggingAspect&period;aroundAdviceForTransferMethods&lpar;&rpar;"&rpar;&semi; &NewLine; jp&period;proceed&lpar;&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&ast;&ast;&ast;&ast;After advised method execution "&plus;jp&period;getSignature&lpar;&rpar;&period;getName&lpar;&rpar;&plus;" LoggingAspect&period;aroundAdviceForTransferMethods&lpar;&rpar;" &rpar;&semi; &NewLine; System&period;out&period;println&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; Declaring named pointcut &NewLine; &ast;&sol; &NewLine; &commat;Pointcut&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;&ast;&lpar;&period;&period;&rpar;&rpar;"&rpar; &NewLine; public void logForAllMethods&lpar;&rpar;&lbrace;&rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Note&colon; <&sol;b>Around advice is special type of advice in the Spring AOP and it has capacity to modify the return of advised method and has full to invoke advised method&period; And never forget to use ProceedingJoinPoint as parameter and must call ProceedingJoinPoint&period;proceed&lpar;&rpar;&semi; method&comma; else the advised method will never be executed&period;<&sol;p>&NewLine;<h3><b>Declare Pointcut expressions<&sol;b><&sol;h3>&NewLine;<p><b>&num;1&period;<&sol;b> In this expression&comma; We have declared around advice&comma; it is valid for all public methods with any number arguments of any type and any return type&comma; for all classes in the <b>com&period;doj&period;aopapp&period;service<&sol;b> package&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Around&lpar;"execution&lpar;&ast; com&period;doj&period;aopapp&period;service&period;&ast;&period;&ast;&lpar;&period;&period;&rpar;&rpar;"&rpar; &NewLine;<&sol;pre>&NewLine;<p><b>&num;2&period;<&sol;b> In this expression&comma; We have declared around advice&comma; it is valid for all public methods whose name is transfer&lpar;&rpar; with taking three arguments of any type and any return type&comma; for all classes in the <b>com&period;doj&period;aopapp&period;service<&sol;b> package&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&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;<&sol;pre>&NewLine;<h2><b>Spring AOP AspectJ &commat;Around Annotation Example<&sol;b><&sol;h2>&NewLine;<p>Now let&&num;8217&semi;s see complete example of Spring AOP aspectj &commat;Around annotation&period;<&sol;p>&NewLine;<h3><b>Spring AOP and AspectJ Maven Dependencies<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">&lt&semi;properties&gt&semi; &NewLine; &lt&semi;spring&period;version&gt&semi;4&period;3&period;7&period;RELEASE&lt&semi;&sol;spring&period;version&gt&semi; &NewLine; &lt&semi;aspectj&period;version&gt&semi;1&period;8&period;9&lt&semi;&sol;aspectj&period;version&gt&semi; &NewLine; &lt&semi;&sol;properties&gt&semi; &NewLine; &NewLine; &lt&semi;dependencies&gt&semi; &NewLine; &lt&semi;dependency&gt&semi; &NewLine; &lt&semi;groupId&gt&semi;org&period;springframework&lt&semi;&sol;groupId&gt&semi; &NewLine; &lt&semi;artifactId&gt&semi;spring-context&lt&semi;&sol;artifactId&gt&semi; &NewLine; &lt&semi;version&gt&semi;&dollar;&lbrace;spring&period;version&rcub;&lt&semi;&sol;version&gt&semi; &NewLine; &lt&semi;&sol;dependency&gt&semi; &NewLine; &lt&semi;dependency&gt&semi; &NewLine; &lt&semi;groupId&gt&semi;org&period;springframework&lt&semi;&sol;groupId&gt&semi; &NewLine; &lt&semi;artifactId&gt&semi;spring-context-support&lt&semi;&sol;artifactId&gt&semi; &NewLine; &lt&semi;version&gt&semi;&dollar;&lbrace;spring&period;version&rcub;&lt&semi;&sol;version&gt&semi; &NewLine; &lt&semi;&sol;dependency&gt&semi; &NewLine; &lt&semi;dependency&gt&semi; &NewLine; &lt&semi;groupId&gt&semi;org&period;springframework&lt&semi;&sol;groupId&gt&semi; &NewLine; &lt&semi;artifactId&gt&semi;spring-aop&lt&semi;&sol;artifactId&gt&semi; &NewLine; &lt&semi;version&gt&semi;&dollar;&lbrace;spring&period;version&rcub;&lt&semi;&sol;version&gt&semi; &NewLine; &lt&semi;&sol;dependency&gt&semi; &NewLine; &NewLine; &lt&semi;dependency&gt&semi; &NewLine; &lt&semi;groupId&gt&semi;org&period;aspectj&lt&semi;&sol;groupId&gt&semi; &NewLine; &lt&semi;artifactId&gt&semi;aspectjrt&lt&semi;&sol;artifactId&gt&semi; &NewLine; &lt&semi;version&gt&semi;&dollar;&lbrace;aspectj&period;version&rcub;&lt&semi;&sol;version&gt&semi; &NewLine; &lt&semi;&sol;dependency&gt&semi; &NewLine; &lt&semi;dependency&gt&semi; &NewLine; &lt&semi;groupId&gt&semi;org&period;aspectj&lt&semi;&sol;groupId&gt&semi; &NewLine; &lt&semi;artifactId&gt&semi;aspectjweaver&lt&semi;&sol;artifactId&gt&semi; &NewLine; &lt&semi;version&gt&semi;&dollar;&lbrace;aspectj&period;version&rcub;&lt&semi;&sol;version&gt&semi; &NewLine; &lt&semi;&sol;dependency&gt&semi; &NewLine; &lt&semi;&sol;dependencies&gt&semi; &NewLine;<&sol;pre>&NewLine;<h3><b>ApplicationContext Configuration file based on Java Config <&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>&num;1&period; Enabling &commat;AspectJ using &commat;EnableAspectJAutoProxy Annotation&colon;<&sol;b><&sol;h3>&NewLine;<p>Spring AOP provides an annotation to enable &commat;AspectJ support in the application&period; By default spring framework doesn&&num;8217&semi;t create any proxy for any advice&comma; so we have to enable by using &commat;EnableAspectJAutoProxy annotation&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&commat;Configuration &NewLine;&commat;EnableAspectJAutoProxy &NewLine;public class AppConfig &lbrace; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h3><b>&num;2&period; Enabling &commat;AspectJ using &lt&semi;aop&colon;aspectj-autoproxy&sol;&gt&semi; in XML Configuration&colon;<&sol;b><&sol;h3>&NewLine;<p>&lt&semi;aop&colon;aspectj-autoproxy&sol;&gt&semi; namespace is equivalent to &commat;EnableAspectJAutoProxy annotation to enable &commat;AspectJ support in the application in XML configuration&period; Let&&num;8217&semi;s see how to use this namespace &lt&semi;aop&colon;aspectj-autoproxy&sol;&gt&semi;&period;<&sol;p>&NewLine;<pre class&equals;"highlight">&lt&semi;&excl;-- Enable &commat;AspectJ annotation support --&gt&semi; &NewLine; &lt&semi;aop&colon;aspectj-autoproxy&sol;&gt&semi; &NewLine;<&sol;pre>&NewLine;<h3><b>Target method of Service class on which aspects needs to apply<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;TransferService&period;java<&sol;b><&sol;h3>&NewLine;<pre class&equals;"highlight">&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;doj&period;aopapp&period;service&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public interface TransferService &lbrace; &NewLine; &NewLine; void transfer&lpar;String accountA&comma; String accountB&comma; Long amount&rpar;&semi; &NewLine; &NewLine; Double checkBalance&lpar;String account&rpar;&semi; &NewLine; &NewLine; Long withdrawal&lpar;String account&comma; Long amount&rpar;&semi; &NewLine; &NewLine; void diposite&lpar;String account&comma; Long amount&rpar;&semi; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p><b>TransferServiceImpl&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;service&semi; &NewLine; &NewLine;import org&period;springframework&period;stereotype&period;Service&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;Service &NewLine;public class TransferServiceImpl implements TransferService &lbrace; &NewLine; &NewLine; &commat;Override &NewLine; public void transfer&lpar;String accountA&comma; String accountB&comma; Long amount&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;amount&plus;" Amount trasferring from "&plus;accountA&plus;" to "&plus;accountB&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;Override &NewLine; public Double checkBalance&lpar;String account&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Available balance&colon; 50000"&rpar;&semi; &NewLine; return new Double&lpar;50000&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;Override &NewLine; public Long withdrawal&lpar;String account&comma; Long amount&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;"Withdrawal amount&colon; " &plus;amount&rpar;&semi; &NewLine; return amount&semi; &NewLine; &rcub; &NewLine; &NewLine; &commat;Override &NewLine; public void diposite&lpar;String account&comma; Long amount&rpar; &lbrace; &NewLine; System&period;out&period;println&lpar;amount&plus;" Amount has been diposited to "&plus;account&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3><b>Aspect class&colon;<&sol;b><&sol;h3>&NewLine;<p>LoggingAspect&period;java as given above in this tutorial&period;<&sol;p>&NewLine;<p><b>Test Class for Spring AspectJ Configuration and Execution<&sol;b><&sol;p>&NewLine;<p>Let&&num;8217&semi;s execute following test class and analyse the output on the console&period;<&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;checkBalance&lpar;"accountA"&rpar;&semi; &NewLine; transferService&period;transfer&lpar;"accountA"&comma; "accountB"&comma; 50000l&rpar;&semi; &NewLine; transferService&period;diposite&lpar;"accountA"&comma; 50000l&rpar;&semi; &NewLine; transferService&period;withdrawal&lpar;"accountB"&comma; 40000l&rpar;&semi; &NewLine; applicationContext&period;close&lpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<h3><b>Output on the Console&colon;<&sol;b><&sol;h3>&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;">&NewLine;<p>Mar 09&comma; 2017 11&colon;49&colon;10 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;Thu Mar 09 23&colon;49&colon;10 IST 2017&rsqb;&semi; root of context hierarchy<br &sol;>&NewLine;&ast;&ast;&ast;&ast;Before advised method execution checkBalance LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<br &sol;>&NewLine;Available balance&colon; 50000<br &sol;>&NewLine;&ast;&ast;&ast;&ast;After advised method execution checkBalance LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<&sol;p>&NewLine;<p>&ast;&ast;&ast;&ast;Before advised method execution transfer LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<br &sol;>&NewLine;&ast;&ast;&ast;&ast;Before advised method execution transfer LoggingAspect&period;aroundAdviceForTransferMethods&lpar;&rpar;<br &sol;>&NewLine;50000 Amount trasferring from accountA to accountB<br &sol;>&NewLine;&ast;&ast;&ast;&ast;After advised method execution transfer LoggingAspect&period;aroundAdviceForTransferMethods&lpar;&rpar;<&sol;p>&NewLine;<p>&ast;&ast;&ast;&ast;After advised method execution transfer LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<&sol;p>&NewLine;<p>&ast;&ast;&ast;&ast;Before advised method execution diposite LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<br &sol;>&NewLine;50000 Amount has been diposited to accountA<br &sol;>&NewLine;&ast;&ast;&ast;&ast;After advised method execution diposite LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<&sol;p>&NewLine;<p>&ast;&ast;&ast;&ast;Before advised method execution withdrawal LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<br &sol;>&NewLine;Withdrawal amount&colon; 40000<br &sol;>&NewLine;&ast;&ast;&ast;&ast;After advised method execution withdrawal LoggingAspect&period;aroundAdviceForAllMethods&lpar;&rpar;<&sol;p>&NewLine;<p>Mar 09&comma; 2017 11&colon;49&colon;10 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;Thu Mar 09 23&colon;49&colon;10 IST 2017&rsqb;&semi; root of context hierarchy<&sol;p>&NewLine;<&sol;div>&NewLine;<p>As output of above console&comma; every log messages has been executed before and after advised method execution&period;<&sol;p>&NewLine;<h3><b>Project Structure<&sol;b><&sol;h3>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;03&sol;spring-aop-around-advice-annotation-example&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&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-after-throwing-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-around-advice-example-using-xml-config&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; '46'&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