Aspect Oriented Programming 20 september 2006

AOP – An Introduction

Many (if not all) developers have seen this: lines of logging code between business logic code. Separating these kinds of technical code with business logic code is generally good practice. With the introduction of Aspect Oriented Programming (AOP) frameworks for Java, separation of logic is much easier to implement. AOP is particularly useful for separating cross-cutting concerns.

Cross-cutting concerns are concerns that spans every single layer in your application, from the business layer to the database access layer. These concerns are usually not easy to separate from this logic and made modular. You will still see traces of those cross-cutting concerns in these layers. Logging, transaction handling and performance monitoring are good examples of these cross-cutting concerns. Using AOP these can be separated from your code, leaving your code to do exactly what it is supposed to do, and nothing more.

There are many Java AOP frameworks available, with the most popular being AspectJ. Users who use the Spring Framework will know that Spring AOP is an important part of the Spring Framework. JBoss has its own AOP implementation as well.

Users who use AspectJ as their AOP implementation with Eclipse as their development environment can download and use the AspectJ Development Tools (AJDT) for full IDE support when implementing their own aspects.

As mentioned before, AOP is very suitable for separating cross-cutting concerns from your business logic.

Examples of cross-cutting concerns:
• Logging
• Transaction Handling
• Performance Monitoring
• Auditing
• Security

This article will show you how to implement simple cross-cutting concerns using AspectJ.

AspectJ is currently the most popular AOP framework for Java. It uses bytecode weaving to weave aspects into compiled Java code. Since the introduction of version 5.0, AspectJ also introduced Load-time weaving of aspects, which allows your aspects to be weaved in the code when your objects loads the first time. This version also fully supports J2SE 5.0, supporting annotation-style aspects.

AOP terminology
• Join Point: A join point is a certain point in the execution or flow of the application
• Pointcut: A pointcut describes the match between the join point and your application logic
• Advice: The code to execute when the specified pointcut has been encountered
 
Using AspectJ
When thinking in terms of AOP, every single line in your application can be a Join Point. In order to insert an aspect at a certain Join Point, a pointcut should be created. As an example:

public void setAmount(int amount) {
 this.amount = amount;
}

Let’s define a pointcut to check permissions when this method is executed:

public aspect MyAspect {
 pointcut setAmount(): execution(* *.set*(*));

 before(): setAmount() {
  // Check permissions here
 }
}

 

In the above example you can check permissions on every method starting with set, and deny access to that method programmatically if the requirements are not met. Without AOP, this code would either be

• coded into the setAmount method
• coded into the setAmount’s calling method
• coded into a proxy class

When using AOP and AspectJ, the before() advice will be woven into the bytecode, either after compile-time or during load-time if a matching join point is found.

Let’s define a pointcut for some simple performance logging functionality:

public void validateOrder() {
 // Write your validation code here
}

 

Now say you want to know how long order validation takes. As in the previous example, you could have coded it in the validation logic, or used a proxy. Using AOP and AspectJ, you can do it like this:

public aspect ValidationPerfLogger {
 pointcut perflogValidateOrder(): execution(* *.validateOrder());

 around(): perflogValidateOrder() {
  long startTime = System.currentTimeMillis();
  proceed();
  long endTime = System.currentTimeMillis();
  log.info(“Validating Order took “  
  + (endTime – startTime) + “ ms”);

 }
}

 

When your application reaches the validateOrder() method, the perflogValidateOrder() advice will be executed first. The actual validateOrder() method gets executed by calling proceed() in the advice. After the validateOrder() returns, the remaining code of the advice gets executed before returning to the calling method.

Weaving
What are the pros of compile-time and load-time weaving over coding it into your application? Cross-cutting concerns can be removed and added without having to recompile your existing code. This makes introducing AOP to existing projects an attractive option, as functionality can be added without modifying existing code. Also, when certain cross-cutting concerns are no longer needed, they can be removed without having to recompile the code. Just do not weave the aspects with the code to remove them.

The cons of compile-time and load-time weaving? Checking whether your advices are applied to the correct join points can be difficult without the correct tools. Of course, you can still turn on warnings and errors for specific parts of AspectJ when weaving your aspects, solving the problem of having all your advices match at least one join point, but it does not solve the problem of seeing whether all your advices match to all the join points you want it to. Tools like AJDT for Eclipse can solve many of these problems, but it still requires some manual checking to make sure every advice matches every join point.

Conclusion
AOP is a very powerful technology for implementing cross-cutting concerns. With the Spring Framework implementing features such as transaction control using Spring AOP, and JBoss jumping on the AOP bandwagon, the technology has shown its worth. It truly separates your business logic code from all the technical code, resulting in very clean, readable code which does exactly what it should be doing. Once you have used AOP to implement cross-cutting concerns, you probably do not want to do it any other way.

However, be careful not to over-use AOP. Debugging and keeping track of all your aspects can be difficult, and not every concern is suitable for implementation using AOP.

Further information
AspectJ: http://www.eclipse.org/aspectj
AspectJ Development Tools: http://www.eclipse.org/ajdt
Spring Framework: http://www.springframework.org/
JBoss AOP: http://labs.jboss.com/portal/jbossaop/
AOP@Work: http://www.ibm.com

 

Author: Bruce Wat

 

 
< Prev   Next >