Header Ads

ad

Spring AOP

AOP stands for "Aspect Oriented Programming". 
This is one of the most important modules of Spring. 
There are some problems with OOP and AOP complements OOP.

Before going further let's understand two things:

    a) Primary Logics
    b) Secondary Logics

 Primary Logics

The logic that is minimum and mandatory to complete a task is called Primary Logic. Without primary logic task can not be completed..

It is also known as business logic..

eg:- withdraw logic, deposit logic etc..

 Secondary Logics

The logic that supports primary logic and makes primary logic as better logic but optional to have in application is called secondary logic.

Without secondary logic we can also complete the task.That is optional in the application..

It is also known as aspects, middleware services,cross cutting concern etc..

eg:- Security, Loggings, Auditing Transaction management logic etc...


Definition of AOP 

AOP is a way of programming that makes the programmer to separate primary logics of application from secondary logics..

AOP is not replacement of OOP moreover it complements OOP.


OOP Example:
  1. public class BankAccount{
  2.    public boolean withdraw(int ac,int amt)
  3.    {
  4.     -security logic
  5.     -logging logic
  6.     -Transaction logic
  7.     --------------------
  8.     bal=bal-amt;//primary logic
  9.   }

  10.  public boolean deposit(int ac,int amt)
  11.  {
  12.     -security logic
  13.     -logging logic
  14.     -Transaction logic
  15.     --------------------
  16.     bal=bal+amt;//primary logic
  17.   }
  18. }

In OOP we develop business method in the class by mixing up both primary logics and secondary logics, because of that there are problems with OOP as follows:

a) Business method becomes clumsy and heavy.
b) Reusability of secondary logic will be killed
c) No flexibility of enabling and disabling the secondary logics without touching the source code.

To overcome above problems, use AOP that makes the programmer to write primary logic in separate class and secondary logic in separate class..


AOP Example:

  1. public class BankAccount{
  2.    public boolean withdraw(int ac,int amt)
  3.    {
  4.       bal=bal-amt;//primary logic
  5.   }

  6.  public boolean deposit(int ac,int amt)
  7.  {
  8.     bal=bal+amt;//primary logic
  9.   }
  10. }










Advantages of AOP

>> Secondary logics are not mixed with primary logics. There is clean separation.
>> Secondary logics become reusable 
>> By giving instruction to AOP enabled software(Spring AOP,AspectJAOP) we can enable or disable secondary logic

NOTE:- We supply primary and secondary logics to AOP enable software and that software takes both the logics and add them together at run time generating a in-memory sub class(Proxy class)...





No comments