In any application, we will have business logic to be developed and we write multiple classes and methods accordingly to achieve it.
Each class and method we develop represents some functionality of the application.
What if all these classes and methods need to have some common functionality?
We call such functionality or a behavior applicable to the entire application as a Cross cutting concern in Aspect oriented programming.
So such cross cutting concerns which are affecting the entire application should be centralized in one place so that it can be reused and easily managed.
Example of cross cutting concerns are Logging,Transaction management,security management as all these concerns are applicable to the entire application.
Just like Class is the main component of Object oriented programming, Aspect is the main component of Aspect oriented programming.
Why do we need to use AOP ?
Assume we have a class called EmployeeService which has below 5 methods in it
- public Employee getEmployeeById(long employeeId){}
- public List<Employee> getAllEmployees(){}
- public void addEmployee(Employee employee){}
- public void deleteEmployee(long employeeId){}
- public void updateEmployee(Employee employee){}
public Employee getEmployeeById(long employeeId){} public List<Employee> getAllEmployees(){} public void addEmployee(Employee employee){} public void deleteEmployee(long employeeId){} public void updateEmployee(Employee employee){}
Requirement: we need to log the message on each method begin and send notification after each method gets completed.
Solution without AOP:
We can add log statement at the beginning of each method and also notification code at the end of each method
Now if client demands that we don’t need notification after method completion, then we have to remove the notification call from each of these methods.
So this leads to the maintenance problem as we need to search each method on which the notification call is made and we have to remove the notification code.
Solution with AOP:We can write logging and notification code in one central place and configure all the 5 methods to use this code.
Now if client demands that we don’t need notification after method completion, then we just need to remove the notification code from the central location.
So maintenance is very easy now.
Where to use AOP ?
AOP should be used in any scenario where we need to implement the cross cutting concerns like logging,transaction management,security implementation.
AOP core concepts
Aspect:
It is a cross cutting concern which is applied for multiple methods in the application
It can be defined as a normal class which can be configured through spring XML configuration
Or we can define it using @Aspect annotation with spring and AspectJ integration.
If we want to define logging aspect and transaction aspect, we just need to define 2 classes one for logging and one for transaction in the entire application.
Join Point:
It’s a specific point in the application on which the Aspect is applied.
Different joint points are method execution,exception handling,changing value of a variable etc.
Spring AOP supports only the method execution to use as a join point.
Advice:
It is the action taken by an Aspect at a particular join point
In simple words, it is a method which gets executed for the matching join point and point cut.
Pointcut:
These are the expression used to match with the join point to determine whether advice has to be applied for the join point or not.
If the point cut expression matches with the join point then advice will be executed.
Example: if we want to execute some advice on all the methods whose name starts with “m”, then we can define point cut expression for the same.
Spring uses the AspectJ pointcut expression language by default.
Target object: They are the objects being advised by Aspects.
In simple words, objects on which Advices are applied.
Spring AOP is implemented using proxies so target object will always be the proxy object.
AOP proxy:An object created by the Spring AOP framework in order to implement the aspect contracts (advise method executions and so on).
Spring AOP Framework uses default proxy as JDK dynamic proxy but we can use CGLIB proxy by adding its dependency.
Weaving: It is the process of linking Aspects with the other objects to create an advised proxy object.
This can be done at the compile time, or load time or run time.
Spring AOP performs this at the run time.
AOP Advice types
Based on when the Advice is applied, we have 5 Advice types as listed below.
Before:
Advice that executes before the execution of join point methods.
@Before annotation can be used to mark advice type as Before advice.
After(Finally):
Advice that gets executed after the join point method’s execution.
Even if the method throws any exception, After advice will be executed.
@After annotation can be used to mark advice type as After advice.
After Returning:
Advice that gets executed only if the method executes normally,
Advice will not get executed if the method throws any exception.
@AfterReturning annotation can be used to mark advice type as After Returning advice.
After Throwing:
Advice that gets executed only if the method throws an exception.
Advice will not get executed if the method executes normally.
This advice can be used if we want to roll back the transaction declaratively.
@AfterThrowing annotation can be used to mark advice type as After Throwing advice.
Around:
This is the most powerful Advice among all.
This advice surrounds the join point method which means it gets executed before and after the execution of the join point methods.
It is also responsible for choosing whether to proceed to the join point method or to shortcut the advised method execution by returning its own return value or throwing an exception.
@Around annotation can be used to mark advice type as Around advice.
We have many AOP framework providers such as springand AspectJ.
Spring AOP currently supports only method execution join points (advising only the execution of methods on spring beans)
If we need to advise field access and update join points, we need to consider AOP framework such as AspectJ.
Remember Spring AOP internally uses Proxies to apply the advice on the method execution.
Crisp & Clear explanation ..
– Chethu