AOP in Java with AspectJ & Annotation – Part 2
Sep 03
This is the second part of a the AOP tutorial in Java. This tutorial mainly aims to introduce Aspect Oriented Programming (AOP). I’m trying to cover AOP concepts and how to apply it in Java using AspectJ & AspectJ Development Tools (AJDT). Through this part I will cover:
- What is AspectJ Development Tools (AJDT)?
- Installing AJDT
- An example using AJDT
- More about AJDT
- Resources and useful links
- What is AspectJ Development Tools (AJDT)?
- Installing AJDT
- An example using AJDT
- More about AJDT
- Resources and useful links
- I want my AOP!, Part 1
- I want my AOP!, Part 2
- I want my AOP!, Part 3
- Aspect-Oriented Programming in Java
- AspectJ website
- AspectJ FAQ
- Getting Started with AspectJ
- Getting started with AJDT
- AspectJ sample code
AJDT as defined in Eclipse wiki is a set of plug-ins for Eclipse that provide support for aspect-oriented software development using AspectJ within the Eclipse IDE. In other words AJDT enables you to write AspectJ code in Eclipse.
Actually you install AJDT into eclipse just like any other plugin. First thing you need to get the update site URL based on your eclipse version from AJDT’s download section. Now go to eclipse –> Help Menu –> Install New Software and follow the images:
After restarting eclipse you should notice a link called AspectJ development on eclipse Welcome screen
This link contains a great guide and a lot of useful tips to help you while developing with AJDT. Now lets start our first AspectJ project using the newly installed plugin. First of all you will find a new category in creating projects wizard called AspectJ. In order to see it go to File –> New –> Other –> AspectJ and then follow the images to create the project:
Now we are going to create a human class in order to use it in our example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.felfelworld.aj; public class Human { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void whatIsYourName() { System.out.println("My name is " + getName()); } public void whatIsYourAge() { System.out.println("I'm " + getAge() + " years old."); } } |
Create a Main class that uses Human class:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.felfelworld.aj; public class Main { public static void main(String[] args) { Human human = new Human(); human.setName("Osama Felfel"); human.setAge(25); human.whatIsYourName(); human.whatIsYourAge(); } } |
Running the Main class will produce this output:
My name is Osama Felfel
I'm 25 years old.
The next step is to write our AspectJ related code. We will write a code that traces our methods execution. In other words we want to print the executed method name and location. The code will be:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.felfelworld.aj; import org.aspectj.lang.Signature; aspect Trace { pointcut traceMethods() : (execution(* *(..))&& !cflow(within(Trace))); before(): traceMethods(){ Signature sig = thisJoinPointStaticPart.getSignature(); String line = "" + thisJoinPointStaticPart.getSourceLocation().getLine(); String source = thisJoinPointStaticPart.getSourceLocation().getWithinType().getCanonicalName(); System.out.println("Tracing: Calling " + sig.getDeclaringTypeName() + "." + sig.getName() + " line " + line + " in " + source); } after(): traceMethods(){ Signature sig = thisJoinPointStaticPart.getSignature(); System.out.println("Tracing: Method " + sig.getDeclaringTypeName() + "." + sig.getName() + " finished"); } } |
So what happened there? Firstly, the pointcut traceMethods() defines a new pointcut called traceMethods. This pointcut matches execution of every method in every class, as long as the control flow isnβt in the current class (Trace). The latter constraint is to stop an infinite loop occurring.
The before(): part of the class defines advice. This is the code that gets inserted just before the execution of any method.
Running main method again will produce the following output:
Tracing: Calling com.felfelworld.aj.Main.main line 5 in com.felfelworld.aj.Main
Tracing: Calling com.felfelworld.aj.Human.setName line 11 in com.felfelworld.aj.Human
Tracing: Method com.felfelworld.aj.Human.setName finished
Tracing: Calling com.felfelworld.aj.Human.setAge line 19 in com.felfelworld.aj.Human
Tracing: Method com.felfelworld.aj.Human.setAge finished
Tracing: Calling com.felfelworld.aj.Human.whatIsYourName line 23 in com.felfelworld.aj.Human
Tracing: Calling com.felfelworld.aj.Human.getName line 7 in com.felfelworld.aj.Human
Tracing: Method com.felfelworld.aj.Human.getName finished
My name is Osama Felfel
Tracing: Method com.felfelworld.aj.Human.whatIsYourName finished
Tracing: Calling com.felfelworld.aj.Human.whatIsYourAge line 27 in com.felfelworld.aj.Human
Tracing: Calling com.felfelworld.aj.Human.getAge line 15 in com.felfelworld.aj.Human
Tracing: Method com.felfelworld.aj.Human.getAge finished
I'm 25 years old.
Tracing: Method com.felfelworld.aj.Human.whatIsYourAge finished
Tracing: Method com.felfelworld.aj.Main.main finished
AJDT has a lot of ways to help us during writing code. Lets take a look on our eclipse project after we wrote the Trace class:
Previous image shows Trace class. Also you will notice that any affected method by any number of advises will have an indicator that shows the total number of advises affecting that method. A detailed description about each advice will be founded just like this image:
Now as we finished our tutorial I need to help you while digging more about AOP, AspectJ and AJDT. There are a lot of useful resources available on the internet and those are the most valuable ones (from my point of view):
Now it’s your turn to use AspectJ and AJDT. Hope it will be an easy and efficient task. You can download the eclipse project from the following link. Have a nice day π
Great and easy-to-follow intro for complete beginners like myself. Thanks.
You’re welcome.
Very clear and simple tutorial! The screenshots were very useful! Thank you! π
Thank you for the encouraging comment.