AOP in Java with AspectJ & Annotation – Part 1

Jul 28

This is the first part of a tutorial aims to introduce Aspect Oriented Programming (AOP). It will cover AOP concepts how to apply it in Java using AspectJ. Through this part I will cover:

  1. AOP: What, Why and when?
  2. AOP terminologies
  3. AspectJ

The main idea here is to cover AOP & AspectJ as FAST as possible, so let’s start.

  1. AOP: What, Why and when?
  2. AOP as defined in Wikipedia is a programming paradigm which aims to increase modularity by allowing the separation of cross-cutting concerns. By another words AOP tries to isolates secondary or supporting functions from the main program’s business logic.
    But why we need AOP we already have OOP?
    OOP makes the code design more reusable and compact but sometimes it makes the code scattered in number of modules. An example for this is adding a logging message in each of class methods in order to log entering and exiting which makes the logging code scattered in number of methods.
    So, When to use AOP?
    A few symptoms can indicate a problematic implementation of crosscutting concerns using current methodologies. We can broadly classify those symptoms into two categories:

    • Code tangling: Modules in a software system may simultaneously interact with several requirements. For example, oftentimes developers simultaneously think about business logic, performance, synchronization, logging, and security. Such a multitude of requirements results in the simultaneous presence of elements from each concern’s implementation, resulting in code tangling.
    • Code scattering: Since crosscutting concerns, by definition, spread over many modules, related implementations also spread over all those modules. For example, in a system using a database, performance concerns may affect all the modules accessing the database.

  3. AOP terminologies
    • Concerns: Even though most classes in an OO model will perform a single, specific function, they often share common, secondary requirements with other classes. These common requirements are called Concerns. Examples of concerns would be: Logging, Transaction management, Security, Auditing, Locking, … etc
    • Join point: is a point in the execution of a program. Examples: Method invocation or Exception handing
    • Pointcuts: is an expression that selects one or more join points
    • Advice: The code that is executed at a particular joinpoint. Types of Advice are:
      • Before advice, which executes before joinpoint
      • After advice, which executes after joinpoint
      • Around advice, which executes around joinpoint
    • Aspects: The combination of the pointcut and the advice is termed.

  4. AspectJ
    1. Installing AspectJ

    2. First of all go to AspectJ download section and download the last stable JAR and run it (double click on it or from command line using java -jar aspectj-X.X.X.jar). Now just follow these photos:

      Concentrate on the last step of installation in order to know how to add AspectJ into your calsspath. Now test it from command line using ajc command which should return something like:

    3. Writing a normal class
    4. Writing Pointcuts and Advices
    5. Writing test class
    6. Compiling, Running and understanding

    7. In order to run this code we need a compiler that understand aspects, advices and all AOP thing, this compiler would be ajc. So, instead of compiling your source code using javac we will go to the directory that holds all our java work and compile it using this command:
      ajc *.java -source 1.6
      After that we will run our main class “Runner” using the regular java command. The result should be:

      As noticed there are three “Before Methods” lines printed, Why?
      Because we defined our Pointcut to includes all the method in the project, so the code in beforeLogging method got executed three time: before main method, before methodA and before methodB.

Hope this tutorial will make you start thinking about AOP in order to save your time and effort.


  1. Waiting the next part. Go on ya felfel 🙂

  2. Mohamed sokar /

    Thank you for the information, which benefited them

Leave a Reply