Uncategorized

AspectJ – Lessons learned

AspectJ is a powerfull tool and is able to solve some problems that the Java language is not able to. It gives developers flexibility to solve crosscutting concerns like logging, profiling, security and so on. At the same time, AOP can be dangerous to us and may lead to serious problems if used incorrectly.

Over the last 2 years I’ve been using aspects quite a lot and gained some experience on how to use it. In this post I want to share some of the lessons I’ve learned.

Use compile time weaving

Go for the load time weaving unless you really need the power and flexibility of load time weaving. The reason is simply that compile time weaving will give you more predictable runtime performance and better performance as aspects are already weaved into the code.

Use annotation pointcuts

I believe annotations are a good thing and it´s definitely something that will be around for a while. After struggling to some degree with creating pattern based pointcuts I discovered that annotations is a great fit for pointcuts as well. In general, I feel that I have more control over my pointcuts and which parts of my code they advice.

Example

We use SpringWS for exposing web services and a requirement we have is to log the response time for every call to the various services. This can be solved by creating an aspect with a PayloadRoot annotation pointcut:


public pointcut executeService() : execution(@PayloadRoot * *(..));

Be careful when writing logging aspects

This one is more of a general performance tip than an aop tip, but since aspect-based loggng is quite popular it deserves a line or two: Logging is quite costly so you should always check the log level before doing the logging so instead of doing this:


   log.info("log something");

you should do this:


if (log.isInfoEnabled()){
   log.info("log something");
}

Avoid writing recursive aspects

AOP is very much like a sharp knife and if you wrongly, great harm can be caused. On of the most common mistakes to do for beginners is to create recursive aspects. The best way to avoid this is to exclude the aspect from the pointcut, so if you have an aspect named LoggingAspect then you should add the following to your pointcuts


  ... && !within(LoggingAspect)

AspectJ is powerful tool for doing code analysis

Aspects are a good way to solve crosscutting concerns in your applications. However, it may come handy other places as well, for example if you want to detect architecture violations in the code or just simply check whether developers are sys out println instead of the preferred log framework


  package com.jensen.application.policyenforcement;

  public aspect DetectSystemOutErrorUsage {
	declare error : (get(* System.out) || get(* System.err)) && ! within (Test*)
	:	"Use log4j instead";
  }

Eclipse and AspectJ

It is an advantage to run your code in the IDE with aspects enabled. This enables you to test your aspects and reduce cycle time. AJDT is the plugin for AspectJ in eclipse, the latest release is quite stable (I use 1.6.0). If your aspects are located in other jar files then your own project (quite normal if you share aspects between different components), then you need to add these jar files to the aspectj build path in Eclipse

These are some of the lessons I’ve learned. The web is packed with tips and tricks, I use the official AspectJ project quite a lot – http://www.eclipse.org/aspectj/. Also the book AspectJ In Action” by Ramnivas Laddad is easy to read with very good examples.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s