Intro to Aspect Oriented Programming with AspectJ
You have all your project done and ready to rock. After a couple of months, the customer decides to add more features and, guess what, that means you will have to alter significant part of your well structured code with clutter that was not really part of the original specification. If there only was a way to add the extra functionality without altering the code…
And there is! That’s why AOP, Aspect Oriented Programming, exists. Don’t be scared by the name: AOP is nothing more than add extra code to your code without altering your code.
Confusing? Not really. We can do that with a neat java library called AspectJ. Take a simple getter and setter class, let’s say, Car. We have getter and setter for the car color (getColor/setColor), and we would like to call a print statement whenever the color changes, but with a condition: we can’t alter the code of the Car class.
The first step is to create the car class. That can be easily done with the following code:
public class Car {
private String color;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Now all we need is to tell the compiler to execute some extra code whenever the setColor method is invoked. In order for that to work, the compiler needs to know at which point of the program execution it will interfere to insert the extra code. This specific point is called a POINTCUT, and is defined on a file called an ASPECT. The syntax of a pointcut specification for our setColor method would be something like this:
pointcut setColorCall(): call(public void Car.setColor(String));
This simply tells the compiler to create a new pointcut named setColorPointCut whenever the public method setColor is called, with a string parameter. Pointcuts accept wildcard expressions. So if you wanted, for example, to intercept ALL method calls made to the car class, the pointcut would be something like this:
pointcut allCallsToCar(): call(* Car.*(..));
Once the pointcut is ready, we are ready to make the piece of code that we want to be executed. This is called an ADVICE, and it goes like this:
before(): setColorCall() {
System.out.println("The car color has changed!");
}
Basically, it tells the compiler to execute the forementioned code before the pointcut in question is executed. If you are wondering if you could put an after instead of “before”, you are right.
All this code goes into a file that is called the aspect, like this:
public aspect CarAspect
{
pointcut setColorCall(): call(public void Car.setColor(String));
before(): setColorCall() {
System.out.println(”The car color has changed!”);
}
}
I named this file CarAspect.aj. The aj extension means that this is an AspectJ aspect instead of a regular java class. I also created a driver class that will call the Car and see if everything works:
public class CarDriver
{
public static void main(String args[]) {
Car mycar = new Car();
mycar.setColor("blue");
}
}
Make sure you have the AspectJ tools installed, and then you can compile everything using the command:
ajc CarAspect.aj CarDriver.java Car.java
on your OS command line. Also, make sure that you download AspectJ from http://www.eclipse.org/aspectj and put the aspectjrt.jar on your class path, otherwise it won’t work. If you go ahead and execute:
java CarDriver
the output should be:
The car color has changed!
AspectJ is a very powerfull tool, and it can do really impressive things much more interesting than this simple example. I would encourage you to go on http://www.eclipse.org/aspectj and take a look at the docs. There is also a nice eclipse plugin on that same site that helps you a lot when writing aspects for your application.
Joby said,
Wrote on September 27, 2008 @ 3:07 am
Good stuff. Let me hear some cakephp.
Kumar Saurabh said,
Wrote on May 12, 2010 @ 10:43 am
This is really excellent introduction for AspectJ programming.
In small example it explain the main use of AspectJ and how to program for it.
hari said,
Wrote on May 25, 2010 @ 7:41 am
wow, this is simple and clear.. thanks a lot
xjxy said,
Wrote on August 7, 2010 @ 1:25 am
it rocks!