Reg comments3

Aspect oriented programming with Java

A practical introduction

A simple example

As a simple example of an implementation of an Aspect let us implement the world's simplest logging Aspect. To do this we will log what happens during the execution of the classic Hello World Java program. For the purposes of this example, the Hello World program I shall use is presented below:


public class HelloWorld {

        public void print() {
                System.out.println("Hello World");
        public static void main(String [] args) {
                HelloWorld hw = new HelloWorld();

We can now implement an Aspect using AspectJ to provide for system logging. To do this I downloaded the AJDT: AspectJ Development Tools Eclipse Based plug-in (make sure you get the correct version for your Eclipse) from here. This provided me with not only the AspectJ runtime, but also various Wizards and browsers to support AOP development.

I then implemented my logging aspect within Eclipse. A screen dump of this is presented in Figure 1. Note that the file generated has a *.aj extension and that the implementation of the before and after advice is a piece of standard Java code. Before exploring what the implementation does, let us describe in plain English the purpose of this Aspect. Essentially this aspect specifies that before executing the HellWorld.print method, we will printout "Entering print" and after returning normally from the method we will printout "Existing print".

Screenshot showing an AspectJ Logger Aspect

In the example in Figure 1, the whole aspect is called Logger, log is the pointcut that implements the join point for this aspect and there are two advice points. Note that AspectJ makes the following pointcuts available:

  • Method call and execution
  • Constructor call and execution
  • Read/write access to a field
  • Exception handler execution
  • Object and class initialization execution

In the example above, I have defined a single execution pointcut. This specifies that on the execution of the HelloWorld.print method two advices should be applied. One before the method executes and one after the method has returned (other options include running after an exception has been thrown). The advice implements the behaviour of the aspect.

To run the combined application (i.e. the HelloWorld program and the Aspect) within Eclipse, I merely run the HelloWorld application as normal. However, the byte codes of the class and the aspect point cuts are dynamically weaved to create the following output:

Entering print
Hello World
Exiting print

Thus the core behaviour of the HelloWorld program and the behaviour of the logging Aspect have successfully been interwoven in the running system without the need to clutter up the original clean source code with calls to the logging subsystem.

Development methodology

A quick note on AOP development. It is clear to me that to identify the concerns which should be implemented as an Aspect is not necessarily straightforward nor is it directly covered by traditional software engineering methodologies. However, a fairly intuitive approach is to separate the AOP development process into some basic steps. These are essentially:

  • Aspectual decomposition: Decompose systems requirements in order to identify crosscutting and common concerns.
  • Concern implementation: Design and implement each concern as a separate aspect. Test and debug.
  • Aspectual re-composition: Identify the cross cut points within your application where the aspect should be applied.

See the References, below for more information on Aspect Oriented Software Development.


AOP is certainly an important element in the Software Engineers toolbox. It is not, I believe, a replacement for Object Orientation, rather it is an augmentation. Using Java to implement the core business logic of an application is still an excellent approach. Indeed, using Java to implement the core behaviour of an aspect also works very well. What AOP gives you is a way of separating and managing the different aspects of modern, complex, software systems.


  • The AspectJ Programming Guide (a CVS log of updates to this is available here).
  • Aspect-Oriented Programming, Proceedings of the European Conference on Object-Oriented Programming. Kiczales, Gregor, John Lamping, Anurag Mendhekar, Chris Maeda, Cristina Lopes, Jean-Marc Loingtier, and John Irwin (1997). Vol.1241, pp.220-242. This is the seminal paper originating AOP.
  • Aspect-Oriented Software Development. Filman, Robert E.; Elrad, Tzilla; Clarke, Siobhan; Aksit, Mehmet; ISBN 0-32121-976-7; available at Cash 'n' Carrion here.
  • AspectJ in Action: Practical Aspect-Oriented Programming. Laddad, Ramnivas; ISBN 1-93011-093-6; available at Cash 'n' Carrion here.

Sign up to our Newsletter

Get IT in your inbox daily

Biting the hand that feeds IT © 1998–2017