Spring AOP

Writing First AspectJ Program in Spring

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div style&equals;"color&colon; &num;274e13&semi;">&NewLine;<p>Spring AOP &lpar;<b>Aspect-oriented programming<&sol;b>&rpar; framework is used to modularize cross-cutting concerns in aspects&period; Put it simple&comma; it’s just an interceptor to intercept some processes&comma; for example&comma; when a method is execute&comma; Spring AOP can hijack the executing method&comma; and add extra functionality before or after the method execution&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>In Spring AOP&comma; 4 type of advices are supported &colon;<&sol;p>&NewLine;<ul>&NewLine;<li><b>Before advice<&sol;b> – Run before the method execution<&sol;li>&NewLine;<li><b>After returning advice<&sol;b> – Run after the method returns a result<&sol;li>&NewLine;<li><b>After throwing advice<&sol;b> – Run after the method throws an exception<&sol;li>&NewLine;<li><b>Around advice<&sol;b> – Run around the method execution&comma; combine all three advices above&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<h2>1&period; Setting up the project structure&colon;<&sol;h2>&NewLine;<p>Launch your preferred IDE &lpar;I use STS&rpar; and Create New Java Project&period; And then create the file structure as shown below&&num;8230&semi;<&sol;p>&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;2012&sol;07&sol;aopsetup&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<p>You need to download all the JARs that are in <b><i>User Libraries &&num;8220&semi;aspectJ-lib&&num;8221&semi;<&sol;i> <&sol;b>and add them to the project’s class path&period; You can find these JARs here&colon; <a title&equals;"Download Spring" href&equals;"http&colon;&sol;&sol;s3&period;amazonaws&period;com&sol;dist&period;springframework&period;org&sol;release&sol;SPR&sol;spring-framework-2&period;5&period;4&period;zip">Spring<&sol;a>&comma; <a title&equals;"Download Commons-logging" href&equals;"http&colon;&sol;&sol;mirrors&period;ibiblio&period;org&sol;pub&sol;mirrors&sol;maven&sol;commons-logging&sol;jars&sol;commons-logging-1&period;0&period;4&period;jar">Commons-logging<&sol;a>&comma; <a title&equals;"Download AspectJrt" href&equals;"http&colon;&sol;&sol;mirrors&period;ibiblio&period;org&sol;pub&sol;mirrors&sol;maven&sol;aspectj&sol;jars&sol;aspectjrt-1&period;2&period;jar">aspectjrt<&sol;a>&comma; <a title&equals;"download Aspectjweaver" href&equals;"http&colon;&sol;&sol;mirrors&period;ibiblio&period;org&sol;pub&sol;mirrors&sol;maven&sol;aspectj&sol;jars&sol;aspectjweaver-1&period;5&period;3&period;jar">aspectjweaver<&sol;a>&period;<&sol;p>&NewLine;<h2>2&period; Writing our Logging Aspect&colon;<&sol;h2>&NewLine;<&sol;div>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">package com&period;dineshonjava&period;sdnext&period;aop&period;tutorial&period;aspectJ&semi; &NewLine; &NewLine;&commat;Aspect &NewLine;public class LoggingAspect &NewLine;&lbrace; &NewLine; public void loggingAdvice&lpar;&rpar; &NewLine; &lbrace; &NewLine; System&period;out&period;println&lpar;"Advice run&period; Get method is called"&rpar;&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2 style&equals;"color&colon; &num;274e13&semi;">3&period; Configuring Spring AOP and &commat;AspectJ support&colon;<&sol;h2>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">&lt&semi;beans&gt&semi; &NewLine;&period;&period;&period;&period;&period; &NewLine; &lt&semi;bean class&equals;"com&period;dineshonjava&period;sdnext&period;aop&period;tutorial&period;aspectJ&period;LoggingAspect" name&equals;"loggingAspect"&gt&semi;&lt&semi;&sol;bean&gt&semi; &NewLine;&period;&period;&period;&period;&period; &NewLine;&lt&semi;aop&colon;aspectj-autoproxy&gt&semi;&lt&semi;&sol;aop&colon;aspectj-autoproxy&gt&semi; &NewLine;&period;&period;&period;&period;&period; &NewLine;&lt&semi;aop&colon;config&gt&semi; &NewLine; &lt&semi;aop&colon;aspect id&equals;"loggingAspect" ref&equals;"loggingAspect"&gt&semi; &NewLine; &period;&period;&period;&period;&period; &NewLine; &lt&semi;aop&colon;after method&equals;"&period;&period;&period;" pointcut&equals;"&period;&period;&period;"&gt&semi;&lt&semi;&sol;aop&colon;after&gt&semi; &NewLine; &period;&period;&period;&period;&period; &NewLine; &lt&semi;&sol;aop&colon;aspect&gt&semi; &NewLine;&lt&semi;&sol;aop&colon;config&gt&semi; &NewLine;&period;&period;&period;&period; &NewLine;&lt&semi;&sol;beans&gt&semi; &NewLine;<&sol;pre>&NewLine;<div style&equals;"color&colon; &num;274e13&semi;">Spring supports the <b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aspect-annotation&sol;">&commat;AspectJ annotation style<&sol;a> <&sol;b>approach and the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;xml-schema-based-aop-example&sol;"><b>schema-based approach<&sol;b><&sol;a> to implement custom aspects&period; These two approaches have been explained in detail in the following&period;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;274e13&semi;"><&sol;div>&NewLine;<div style&equals;"color&colon; &num;274e13&semi;">&NewLine;<p><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;xml-schema-based-aop-example&sol;">1&period; XML Schema based<&sol;a> &colon;<&sol;b> Aspects are implemented using regular classes along with XML based configuration&period;<&sol;p>&NewLine;<&sol;div>&NewLine;<div style&equals;"color&colon; &num;274e13&semi;">&NewLine;<p><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aspect-annotation&sol;">2&period; &commat;AspectJ based<&sol;a> &colon; <&sol;b>&commat;AspectJ refers to a style of declaring aspects as regular Java classes annotated with Java 5 annotations&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;<&sol;div>&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;setting-up-aop-dependencies&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;xml-schema-based-aop-example&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; '645'&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