Aspect Oriented Programming

Overview : AOP allows you to dynamically modify your static model consisting mainly of business logic to include the code required to fulfill the secondary requirements or in AOP terminology called cross-cutting concerns (i.e. secondary requirements) like auditing, logging, security, exception handling etc without having to modify the original static model (in fact, we don't even need to have the original code). Aspect Oriented Programming is a concept for separating cross-cutting concern into single units called Aspects.   The best possible uses of AOP are like  auditing, logging, security, exception handling etc without having to modify the original static model.

Object Oriented vs Aspect Oriented programming : In OOP real world problems are mapping to Objects. OOP encourages software re-use  using encapsulation, inheritance and polymorphism. It is very difficult to cleanly maintain separate concerns into modules. An attempt to do a minor change in the program design may require several updates to a large number of unrelated modules. With AOP, we start by implementing our project using our OO language (for example, Java), and then we deal separately with crosscutting concerns in our code by implementing aspects. AOP implementation coexists with the OOP by choosing OOP as the base language. AOP addresses each aspect separately in a modular fashion with minimal coupling and duplication of code

Aspect Weaver : The aspect weaver integrates aspects into the locations specified by the software as a pre-compilation step.

Aspect Waver

Above diagram shows , Aspect waver combines .java ( Java Classes ) with .aj (Aspect Class) to create final version of byte code after merging aspect to main code without modifying Java source files.

Aspect Oriented Programming is a concept so it is not bounded to particular programming language.  But reach community got great interest in java language. Following is the list of few AOP + Java implementation

  1. AspectJ
  2. AspectWerkz
  3. Hyper/J

In this post i am going to explain AspectJ implementation.

AOP pillars : Following terminologies are used in AOP

Joint Point : Joint point represent well defined points in program’s execution. Typical join point in AspectJ include

  • method/constructor calls
  • method/constructor execution
  • field get and set
  • exception handler execution
  • static & dynamic initialization

Point Cut : Point cut is a language construct that picks out a set of join points based on defined criteria. The criteria can be explicit function name or wild cards.

Advice : Point cuts are used in he definition of Advice. An advice is AspectJ is used to define additional code to be executed before , after or around joint conditions.

Following example explains all these concepts. Here step by step approach to try first hand on aspect oriented programming.

1. Get AspectJ latest version and install it.

2.  Write a simple Java class

  1: public class TestClass 
  2: {
  3: 	public static void display(){
  4: 		System.out.println("Inside Display");
  5: 	}
  6: 
  7: 	public static void main(String[] args){
  8: 		display();
  9: 	}
 10: }
 11: 

Here we have declared normal java class with simple public void display method as well as main method.


3. Write a simple aspect class

  1: public aspect TestClassAspect 
  2: {
  3: 	//defining pointcut, exection of display() method
  4: 	public pointcut displayCall(): call (public static void display() );
  5: 
  6: 	before() : displayCall(){
  7: 		System.out.println("Inside Before DisplayCall");
  8: 		System.out.println( thisJoinPointStaticPart.getSignature().getName() + " End...");
  9: 	}
 10: 
 11: 	after() : displayCall(){
 12: 		System.out.println("Inside After DisplayCall");
 13: 		System.out.println( thisJoinPointStaticPart.getSignature().getName() + " End...");
 14: 	}
 15: 
 16: }
 17: 

We can define normal methods and variables as well in this aspect class. Here we have defined join points, point cuts and advice. We have defined point cut named  “displayCall” which collects all joint point for method call with signature “public static void display()”. Once we have this point cut declared , we can use this to defining any advice after,before or around this. In next step, two advices have been defined , one is before point cut and another is after point cut. Here point cut is noting but display method call, so this advice will get execute before and after the display method call.


4. Compile Java+aspect class together to get final version of byte code ( .class file)



ajc -cp D:\aspectj1.6\lib\aspectjrt.jar TestClassAspect.aj TestClass.java


5. Run the class file


java -cp D:\aspectj1.6\lib\aspectjrt.jar TestClass


Here is the out :


D:\Learning Material>java -cp .;D:\aspectj1.6\lib\aspectjrt.jar  TestClass
Inside Before DisplayCall
display End...
Inside Display
Inside After DisplayCall
display End...


 


 


Resources :


http://www.developer.com/article.php/3308941


http://en.wikipedia.org/wiki/Aspect-oriented_programming

Comments

Popular posts from this blog

Composite Design Pattern by example

State Design Pattern by Example

Eclipse command framework core expression: Property tester