<p>In this Spring AOP Tutorial we are talking about the <b>Aspect Oriented Programming</b> in Short &#8220;<b>AOP</b>&#8220;, <b>Aspect Oriented Programming </b>is a feature Spring provide in the <a href="https://dineshonjava.com/dependency-injection-in-spring/"><b>Dependency Injection</b></a>, Actually <b>Aspect Oriented Programming </b>is not only feature but also its different style of programming just like as <b>Object Oriented Programming.</b></p>
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><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>
<h2><b>Introduction to Spring AOP</b></h2>
<p>Now I am talking about some other programming before Aspect Oriented Programming&#8230;<br />
<b></b></p>
<h3><b>1. Functional Programming: </b></h3>
<p>In older programming language like C, we have used functional programming style like below in figure.</p>
<div class="separator" style="clear: both; text-align: center;"></div>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/07/functionalprogramming.jpg" border="0" /></div>
<div style="color: #274e13;">As above see In this style of programming writing code into couple of functions and each function perform unit task and each function call another function as see above and after last function execution then program is completed. But in this style of programming the main problem is complexity, it is very messy style of coding to write big project programming.</div>
<div style="color: #274e13;"></div>
<h3 style="color: #274e13;"><b>2. Object Oriented Programming:</b></h3>
<div style="color: #274e13;">In this style of programming we would not think about function when we trying to solve problem by writing code, we would think as individual entities as object when writing the program as below Object A, Object B &; Object C&#8230;</div>
<div style="color: #274e13;"></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/07/oop.jpg" width="640" height="218" border="0" /></div>
<div style="color: #274e13;">Here each object contain member variables and methods to perform individual tasks of each individual entity so this is fine but here is also a problem that not all rectify common procedure in all of the objects as <b>common logging procedure</b> in all as <i><b>logMessage()</b></i> method in all.</div>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/07/commonprocedure.jpg" width="640" height="208" border="0" /></div>
</div>
<div style="color: #274e13;">In above see that <b><i>logMessage() </i></b>method in each objects no matter how many objects are there so this is not good design of programming each object has repeating method. <b>So to solve this type problem we write the separate entity for logger and called in each objects where we want to add read log message as below.</b></div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/07/separatelog.jpg" width="640" height="188" border="0" /></div>
<div style="color: #274e13;"><b> </b>For using this logger entity in each object we have to make <a href="https://dineshonjava.com/dependency-injection-in-spring/">dependency injection</a> with each beans of business classes or we have use inheritance for accessing logger method of Logger class that is good but couple of problems are also there.</div>
<div style="color: #274e13;"></div>
<p>First Problem is doing the design this type of style there too many dependencies with non business object because logger object does not have any business logic in the project its using just for logging with each objects in the project.</p>
<p>PROBLEMS:</p>
<ul style="text-align: left;">
<li>To many relationships with the crosscutting objects.</li>
<li>Code is still required in the all methods</li>
<li>Cannot all be changed at once</li>
</ul>
<div style="color: #274e13;">CROSS CUTTING CONCERNS: Means non business idea or non business logic its not part of our main problem it is related to below&#8230;</div>
<ul style="text-align: left;">
<li style="color: #274e13;">Security</li>
<li style="color: #274e13;">Logging</li>
<li style="color: #274e13;">Transaction</li>
</ul>
<div style="color: #274e13;">To solve the above problems in the Object Oriented Programming we can using Aspect Oriented Programming.</div>
<div style="color: #274e13;"></div>
<div style="color: #274e13;">
<h3><b>3. Aspects Oriented Programming: </b></h3>
<p>In this style of code we are make Aspects means Aspects are also specific classes which some special methods for particular tasks like logging, security and transactions etc.</p>
<p><b><i>Aspect-Oriented Programming</i> (AOP) </b>complements <b>Object-Oriented Programming (OOP)</b> by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the <i>aspect</i>. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects. (Such concerns are often termed <i>crosscutting</i> concerns in AOP literature.)</p>
<p><i><b>Aspect-oriented programming</b></i> entails breaking down program logic into distinct parts (so-called concerns, cohesive areas of functionality). All programming paradigms support some level of grouping and encapsulation of concerns into separate, independent entities by providing abstractions (e.g., procedures, modules, classes, methods) that can be used for implementing, abstracting, and composing these concerns. But some concerns defy these forms of implementation, and are called crosscutting concerns because they &#8220;cut across&#8221; multiple abstractions in a program.</p>
</div>
<div style="color: #274e13;">First we make different Aspects&#8230;</div>
<div style="color: #274e13;"><b> a. Logging Aspect</b></div>
<div style="color: #274e13;"><b> b. Transaction Aspect</b></div>
<div style="color: #274e13;"><b> c. Security Aspect</b></div>
<div style="color: #274e13;">
<p> etc&#8230; and configure with each of objects as per as our requirement with <b>Aspect Configuration file </b>as see below in figure&#8230;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/07/aspect.jpg" border="0" /></div>
<p><b>Aspect Configuration file: </b>Its responsible for configuration for all Aspects with the all object where we want to use. Its configure suppose <b>Logging Aspect</b> for a method in <b>Object A</b> <b>Before or After</b> execution of that method,<br />
its just like the&#8230;..<br />
&#8211;<b>Servlet Filter in Servlet Configuration </b><br />
<b> -Trigger in Database</b><br />
<b> -Interceptors in Struts or in Spring MVC.</b></p>
<p><b>Aspect Configuration tells which aspect apply which method of which class. </b><b>Aspect Configuration solve our three problems of Object Oriented Programming</b></p>
<ul style="text-align: left;">
<li><b>To many relationships with the crosscutting objects</b>&#8211; Only single configuration required for every object where we want to use the crosscutting object like Logging Aspect with using Aspect Configuration file</li>
<li><b style="color: #274e13;">Code is still required in the all methods- </b>No need to code required in all method just put that method on the Aspect Configuration file then code automatically associated with that method and execute Before or After execution of Target Method.</li>
<li><b style="color: #274e13;">Cannot all be changed at once- </b>We can all be changed at once by using Aspect Configuration file.</li>
</ul>
</div>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/07/aspect2.jpg" border="0" /></div>
<p> ;</p>
<div style="color: #274e13;">There are only two Steps for using Aspects:</div>
<ol style="text-align: left;">
<li style="color: #274e13;"><b>Write Aspects</b></li>
<li><b>Configure Aspects where the aspects apply </b></li>
</ol>
<h3 style="color: #274e13; text-align: left;"><b>AOP Terminologies:</b></h3>
<div class="itemizedlist" style="color: #274e13;">
<ul >
<li><b><i>Aspect</i></b>: A modularization of a concern that cuts across multiple objects. Transaction management is a good example of a crosscutting concern in J2EE applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (<b>@AspectJ style</b>).</li>
<li><b><i>Join point</i>: </b>A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point <i>always</i> represents a method execution. Join point information is available in advice bodies by declaring a parameter of type <b>org.aspectj.lang.JoinPoint.</b></li>
<li><b><i>Advice</i>:</b> Action taken by an aspect at a particular join point. Different types of advice include &#8220;around,&#8221; &#8220;before&#8221; and &#8220;after&#8221; advice. Advice types are discussed below. Many AOP frameworks, including Spring, model an advice as an <i>interceptor</i>, maintaining a chain of interceptors &#8220;around&#8221; the join point.</li>
<li><b><i>Pointcut</i>:</b> A predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP: Spring uses the AspectJ pointcut language by default.</li>
<li><b><i>Introduction</i>: </b>(Also known as an inter-type declaration). Declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any proxied object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching.</li>
<li><b><i>Target object</i>:</b> Object being advised by one or more aspects. Also referred to as the <i>advised</i> object. Since Spring AOP is implemented using runtime proxies, this object will always be a <i>proxied</i> object.</li>
<li><b><i>AOP proxy</i>:</b> An object created by the AOP framework in order to implement the aspect contracts (advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy. <i>Proxy creation is transparent to users of the schema-based and @AspectJ styles of aspect declaration introduced in Spring 2.0.</i></li>
<li><b><i>Weaving</i>:</b> Linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.</li>
</ul>
</div>
<h3 style="color: #274e13; text-align: left;"><b>Types of advice:</b></h3>
<div class="itemizedlist" style="color: #274e13;">
<ul >
<li><b><i>Before advice</i>: </b>Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).</li>
<li><b><i>After returning advice</i>: </b>Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.</li>
<li><b><i>After throwing advice</i>: </b>Advice to be executed if a method exits by throwing an exception.</li>
<li><b><i>After (finally) advice</i>: </b>Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).</li>
<li><b><i>Around advice</i>: </b>Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. 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.</li>
</ul>
<div>
<p> ;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><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/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>
</div>
<div style="text-align: center;"><b><;<; </b><b><a href="https://dineshonjava.com/event-handling-in-spring/">Event Handling in Spring</a></b><b> |<a href="https://dineshonjava.com/spring-tutorial/">index</a>| <a href="https://dineshonjava.com/setting-up-aop-dependencies/">Setting up AOP in Spring</a>>;>;</b></div>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/event-handling-in-spring/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/setting-up-aop-dependencies/">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: '647'});
});
</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…