Friday, June 14, 2013

AOP (Aspect Oriented Programming) Overview




1.      AOP (Aspect Oriented Programming)

It is a programming paradigm to increase modularity by separating the cross cutting concerns (Logging, Transaction Management, Security, Validation, Exception Handling etc.) from the main business logic.
Without AOP, the cross cutting concerns will be scattered throughout the application code (across numerous methods) and will result in:
1.       Hard to maintain the code.
2.       Boiler-plate code
3.       Affects reusability

With AOP:
1.       Modularize the cross cutting concerns into aspects
a.        Easy to maintain
                                                                                       i.      Reduce time to make changes
                                                                                      ii.      Reduce effort to make changes
b.       No boiler plate code
c.        Reusability
d.       Provides loose coupling
                                                                                       i.      Aspects can be applied declaratively to the classes.
                                                                                      ii.      It helps to apply aspects without core application’s knowlegde

2.       The developer can fully concentrate on the business logic (core logic) and leave the cross cutting concerns to AOP.
a.        Using AOP, Cross-Cutting concerns like Logging, Transaction Management, Security, Validation and Exception Handling can be modularized into separate aspects with advice methods and the business methods/logic are advised with these aspects wherever required.

2.      AOP Concepts

Aspect: It’s a module which encapsulates the advice and the point-cuts. We can define separate aspect for each cross cutting concerns. For example, define an aspect for Transaction Management; define another aspect for Logging etc.
Advice: It’s the logic/behavior we need to add/include at the join-points in your application business logic classes. For example the actions we need to perform on logging be defined as an advice in the Aspect for logging. This behavior can be added at the join-points in your application code. We can have multiple advices inside an aspect, depending on the requirement.
Point-cut: It is the expression, which defines which advice needs to be added at what points in your application. For example, if,on call of all the service methods, they need to log a statement to a log file, then we should define a point-cut on the advice method of your Logging Aspect, saying that all methods in the service classes should be advised with this advice for logging to the log file.
Join-Point:It is the point in your application code where you want to add the cross cutting behavior (the advice method of the Aspect).
Target Object: It is the object that is being advised by the aspect. This is the object which contains the join point, on which the behavior (advice method of the aspect) needs to be plugin/added.

3.      Different types of Advices

3.1     Before Advice:

It’s the simplest advice type. This advice is executed before the execution of the advised methods in the application. That is this advice gets executed before the execution of the join point in the application. You can define the advice using the annotation @Before.
Limitations:
1.       You cannot control the execution flow using this advice; for example, if we want to terminate the execution on a given condition (if a flag is false) without executing the joint-point method/advised method, it’s not possible with @Before advice.
2.       You cannot modify the advised method arguments.
3.       You cannot modify/access the returned value from the advised method, since this advice is getting executed before the execution of the advised method.
Usage:
1.       This Advice can be used for logging purpose. To keep track of the method invocation.

3.2     After Advice

This advice gets executed once the execution of the advised method is completed.
There are three types of After Advice based on the outcome of the execution of the join point in the application.
1.       After Returning
-          This advice gets executed once the join point execution returns successfully.
2.       After (finally)
-          This advice gets executed regardless of the outcome of the joint point execution. That means this advice will get invoked on exception as well as normal execution return.
3.       After Throwing
-          This advice gets invoked when the advised method execution is terminated with an exception.
Limitation:
1.       You cannot control the execution flow using this advice; for example, if we want to terminate the execution on a given condition (if a flag is false) without executing the joint-point method/advised method, it’s not possible with @Before advice.
2.       You cannot modify the advised method arguments.
3.       You cannot modify/access the returned value from the advised method, since this advice is getting executed before the execution of the advised method.
Usage:
1.       Exception handling can be done using the After Throwing  advice
a.        You can perform the notification/logging on the exception details using the advice.
2.       Use After Returning Advice for logging the return value from the advised methods.
Note: Always good to use specific Advices (After Returning/ After Throwing) instead of the generic After Advice (After (finally)).

3.3     Around Advice

This advice will get invoked around the execution of the join-point in the application. That means before and after the execution of the join point.
1.       This advice allows you to modify the join point method/advised method arguments and passes the modified argument to the join point method.
2.       It also allows you to modify the return value from the join point and pass it to the caller.
3.       It can control the execution flow. That means you can terminate the execution without executing the join point
4.       It can be used to execute the join point any number of times. So can be used for retry mechanisms, for any idempotent operations.
Usage:
1.       Handle XSS vulnerability check on the input parameters to your controller methods.
2.       Log the time taken to execute a given join point method.
3.       Make changes to the state of the object (argument) to the join point method.
4.       Add more details to the join point returned object

4.      AOP Implementation Frameworks

1.       Spring AOP
It is implemented in pure Java and is a proxy based AOP. So it doesn’t require a special compilation process or a special class loader.
2.       AspectJ
It’s a byte-code weaving based AOP. It requires special compilation process and a special class loader.
3.       Jboss AOP
It is a pure java byte code weaving based AOP. It requires a special compiler called Annotation Compiler provided by JBoss AOP to process.

No comments:

Post a Comment