I made a change in the blogger configuration to ease the later work when blogging. It is possible that older entries are not correctly formatted.

Wednesday 12 December 2007

AspectJ: Aspect Oriented Programming

Since Aspect orientation is gaining importance, as well as being very useful, I sum up here a few things about aspect orientation. This short explanation uses mainly the tutorial from: http://www.tomjanofsky.com/aspectj_tutorial.html
Aspect oriented programming is used when some action should be performed at specific moment of the program, but do not influence the way of running of the program. The typical examples is logging, caching, history, timing, authentication...
There is a number of new concept to learn: crosscutting concern, join point, point cuts, advice.
  • a join point is a point of the execution of the program
  • a point cut is a way of selecting join points in the program
  • an advice is the code to be executed when reaching the point cut
there are many ways of defining the point cut, e.g. during method call, or initialisation, or calls for setting or getting the value of a field, class execution or object initialisation An example: pointcut foo() : call (* * BankAccount.* (*)) defines a point cut foo applying advice when any method with one parameter is called on an object of class BankAccount. The syntax for deining an aspect is:
public aspect TraceAspect {
pointcut trace () : call (* * (..)) && ! within(TraceAspect);
before() : trace() {
System.out.println("ADV:"+thisJoinPoint.getSignature());
}
}
Note the keyword aspect, as well as before() ... the advice is:
System.out.println("ADV:"+thisJoinPoint.getSignature());
and is called before a method when not within( TraceAspect) Since there are already many aspect oriented programming with the spring framework, it might be prefarable to use that.