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:

  1. What is AspectJ Development Tools (AJDT)?
  2. Installing AJDT
  3. An example using AJDT
  4. More about AJDT
  5. Resources and useful links

  1. What is AspectJ Development Tools (AJDT)?
  2. 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.

  3. Installing AJDT
  4. 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:

    install new plugin into eclipse
    adding new update link to eclipse
    entering update site link for AJDT
    choosing plugins to download
    confirming chose plugins
    accepting license agreement
    restarting eclipse after new plugin installation

  5. An example using AJDT
  6. After restarting eclipse you should notice a link called AspectJ development on eclipse Welcome screen

    first run after installing AJDT

    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:

    creating AspectJ project
    AspectJ project properties
    confirming opening AspectJ perspective

    Now we are going to create a human class in order to use it in our example:

    Create a Main class that uses Human class:

    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:

    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

  7. More about AJDT
  8. 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:

    notes about AJDT in eclipse

    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:

    notes about AJDT in eclipse

  9. Resources and useful links
  10. 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 πŸ™‚

    Download eclipse project


  1. Regenbogenfisch /

    Great and easy-to-follow intro for complete beginners like myself. Thanks.

  2. Emilie /

    Very clear and simple tutorial! The screenshots were very useful! Thank you! πŸ™‚

Leave a Reply