Thursday, April 16, 2009

Difference between Static AOP and Dynamic AOP

If you have none knowledge on aspect oriented programming you should continue this article to have a basic knowledge on the topic.

AOP is a complement of object oriented programming, not a compete. In few words, with aop, you can execute a code (called advice in aop termology) in a defined point (called joinpoint) during the execution of your application.

An example to joinpoint can be execution of a method. You can group a collection of all method calls in a particular package (so this was an example to pointcut, so group of joinpoints are called pointcut). 

The combination of advice (the code which is going to be executed on a joinpoints) and pointcuts are called aspect.

There are other termologies involved too but you got the basic idea. For example now you know that with aop you can select the methods (pointcuts) and configure your logging class to be invoked (advice) before and after each method call in your application. So you do not need to go each method and change those methods just to measure the time spent, you can achieve this more simply with aop. Also your methods will not contain code which is not related with their business.

Term weaving refers to the process of inserting aspects into the application code at the appropriate point. The difference of Static and Dynamic AOP occurs here.

In static aop, weaving occurs in compilation time. Thus means, the advices are included to compiled bytecode.

In dynamic aop, weaving occurs on the fly while bytecode is instrumented. So there are no any bytecodes related with aop in the compiled code, but they are added at runtime.

Static aop should perform better since there is no need to special handling in runtime. In the other hand dynamic aop is more flexiable becouse you do not need to re-compile your code when your aspects changes.

You may choose these links to do a hello world with aop:

Hello world with AspectJ (static aop):
Hello world with Spring AOP (dynamic aop):