Monday, November 14, 2011

Aspect Oriented Programming (AOP)

Enables modularization of cross-cutting concerns (to avoid tangling and to eliminate scattering)

Concepts:

1. Advice - The actual work AOP will perform on a given object (around, before, after)
2. Join Point - places within your code that are eligible for advice; method execution
3. Pointcut - description of the joinpoint you want to advise; you list the criteria of the joinpoint you are interested in advising here. Eg. execution(public * *(..))
4. Aspect - functionality you actually want to add to an object (e.g. logging, security)

Aspects use Pointcuts to find Joinpoints to give Advice to ...



How AOP works?

AOP works by separating common concerns across the layers in separate classes and names them Aspect.
Aspects have APIs which aim to solve a common problem, like logging, and exception handling.
These are then invoked while intercepting the actual method invocation. Such APIs are termed Advice.
Advices are interceptors, which intercept original method invocation and wrap it around with handling of cross cutting concerns.
Now, for applying Advices to methods, AOP provides artifacts named JoinPoint.
Using a JoinPoint we could defne, which API invocation would be wrapped around by what Advice.

Weaving is a mechanism by which the aspects are applied to the designated classes and methods.
Weaving is generally done by generating byte code for the interceptors, using various libraries for byte code generation libraries, such as JavaAssist, BCEL, and ASM.
There are three places, any of which could be used to generate the bytecode. These are while compiling the code (Compile Time Weaving), loading the application known as (Load Time Weaving) and (Runtime Time Weaving) while actual method invocation is running.


Ref:
Spring doc
Learning Google Guice

No comments:

Post a Comment