The Wayback Machine - https://web.archive.org/web/20110810134817/http://www.developer.com:80/java/5-reasons-java-developers-should-learn-and-use-aspectj.html
August 10, 2011
RSS RSS feed

5 Reasons Java Developers Should Learn and Use AspectJ

Aspect-Oriented Programming (AOP) is a programming paradigm which focuses on modularizing system-level concerns (like logging, transaction management, security, performance monitoring, etc.) in the applications. In AOP language, these system-level concerns are called "crosscutting concerns" because they crosscut all the layers of the application.

AspectJ, a compatible extension to the Java programming language, is one implementation of AOP. AspectJ is very widely used in a lot of Java frameworks (like Spring), but still most developers do not know AspectJ. Developers often think that AspectJ is difficult to learn or it makes your code complex, and they decide not to learn this very powerful and useful technology. In this article, I will write down five reasons why I think Java developer should learn and use AspectJ.

AspectJ Is Easy to Learn

AspectJ is not difficult to learn. It has three core concepts: Join point, Pointcut, and Advice. Join points are the points in the execution of a program where you need to apply the cross cutting behavior. For example, execution of a method, call to a method or a field access are all valid join points. Pointcut allows developers to write expressions to select join points of interest and capture the join point context information. Advice allows a developer to execute some code at the join points selected by pointcut. AspectJ concepts are 3Ws--where (Join point), which (Pointcut), and what (Advice).

Let's use an example to make it clearer. Consider a scenario where you have to validate that argument to a method is not null. Without AOP you will do a null check in all the methods of interest and will throw an exception if the argument is null.

public class MyService {

public void process(ProcessRequest request){
if(request == null){
throw new IllegalArgumentException("Request can not be null");
}
// proceed forward
}
}

In the code shown above, we are validating that request should not be null before processing the request. If the request is null, we throw IllegalArgumentException. I can remove the validation logic from this class and move it a Validator class, but still I would have to make a call to validator class. You could have ten such classes where you are just doing null validation. So null validation is a crosscutting concern, which can be better implemented using AspectJ. The Join point is in the MyService class process method because it is the place where we would like to do validation (applying advice).

Now let's write an aspect that would declare a pointcut for selecting the process method and an advice that will do the validation before the join point is executed.

public aspect NullValidationAspect { // 1

pointcut validate(ProcessRequest request) : execution(public void MyService.process(ProcessRequest)) && args(request); //2

before(ProcessRequest request) : validate(request) { //3
if(request == null){
throw new IllegalArgumentException("Request can not be null");
}
}
}

Let's talk about the aspect shown above.

  1. Line 1 corresponds to an Aspect declaration. The aspect keyword specifies that the construct is an Aspect.
  2. Line 2 specifies a pointcut. The pointcut construct specifies the name of the pointcut (i.e. validate, a pointcut expression to select the validate method such as execution (public void MyService.process(ProcessRequest), and an args() construct to capture the method arguments). The pointcut expression language is very rich and selects a variety of join points like call to a method, call or execution of a constructor, object initialization, static initialization. Please refer to AspectJ documentation for more information.
  3. Line 3 refers to a before advice. A before advice runs before the join point execution. There are other types of advices also, such as after advice, which runs after the execution of join point code, around advice which surrounds the join point so you can do processing before and after the endpoint. In the advice, we will throw exception if the capture argument (i.e. ProcessRequest) is null.

I have given a very small and easy introduction to AspectJ here. If you can learn these concepts, then you can very easily use AspectJ in your projects. You can learn about other features of AspectJ like static crosscutting, advance pointcut expressions, etc. as you become comfortable working with AspectJ. I think once you start using basic features of AspectJ it will become very easy and natural to use advanced features. The purpose of this article is not to help you build applications using AspectJ, but to motivate you to learn AspectJ.

Another thing that makes learning AspectJ very easy is a great book--AspectJ in Action. This book teaches you how to think in aspects and shows how you can solve real life problems with AspectJ. It is a must buy if you want to learn AspectJ.

AspectJ Provides a Standard Mechanism to Handle a Crosscutting Concern

In the example shown above, we are doing a null validation and throwing an IllegalArgumentException when the request is null. This way we make sure that whenever an argument is null, we get the same uniform behavior. This leads to uniformity in your application code and prevents bugs from creeping into the application. It eliminates the chance of missing the validation and throwing another exception. The advantage of having a uniform way become more apparent when you are handling a crosscutting concern like Exception Handling. Having the exception handling code in the aspect makes your business logic cleaner and prevents developers from doing it differently or missing it.


Tags: AspectJ, aspect-oriented programming



Networking Solutions
Sitemap | Contact Us