Feeds

Aspect oriented programming with Java

A practical introduction

Choosing a cloud hosting partner with confidence

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:

package com.reg.dev.aspects;

public class HelloWorld {

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

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.

Summary

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.

References

  • 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.

Intelligent flash storage arrays

More from The Register

next story
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
UNIX greybeards threaten Debian fork over systemd plan
'Veteran Unix Admins' fear desktop emphasis is betraying open source
Sign off my IT project or I’ll PHONE your MUM
Honestly, it’s a piece of piss
Return of the Jedi – Apache reclaims web server crown
.london, .hamburg and .公司 - that's .com in Chinese - storm the web server charts
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
Torvalds CONFESSES: 'I'm pretty good at alienating devs'
Admits to 'a metric ****load' of mistakes during work with Linux collaborators
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.