Wednesday, January 13th, 2010

Use cases of Aspect Oriented Programming

In my last article about Aspect Oriented Programming (AOP), I explained how to use Spring AOP for profiling method execution time. When the article was posted in DZone, it got a comment from an user saying that

Profiling and logging seem to be to only imaginable AOP use cases.

But, I think it is not. Because AOP is an extensive area where a lot of examples can be shown as use cases of AOP. Here, “Use cases” are nothing but the cross cutting concerns in your application. That is, the functionalities that are used by many part of your application. Logging and Profiling are the best examples and can be easily explained for the cross cutting concerns. That is why in most of the AOP tutorials you can see a logging example or profiling.

Anyway, I tried to do some search on the various AOP cross-cutting concerns (i.e. the use cases) that can be found in an J2EE applications. Here is the list of them. Feel free to add if I’m missing/wrongly added something.

List of AOP Use Cases or Cross cutting concerns:

  • Transaction Management – in an enterprise application, you can manage the transactional requests using AOP. For example, using the Around advice, you can wrap the request processing method and if the method fails for some reason, in your Advice, you can roll back the particular transaction.
  • Access control or Security - in case you want to restrict the access to your method to a set of users, you can add these checks to the Before advice and verify the access credentials of the users before letting them to access your methods.
  • Managing Quotas for your API - if your API is being used by many third party applications with predefined quotas, you can limit the access based the usage in your AOP advice. If the usage is below the allowed limit, the Advice can let the user to call the API or blocks it otherwise. This is something similar to the Google App Engine / Amazon services quota usages. But I’m not sure how Google/Amazon has implemented the quota limiting.
  • Exception Wrapping – if you want to wrap a exception, thrown from your business methods, into a Common exception object and throw it to the top level layers, you can use After throwing advice to do the task. For example, if you want to wrap all the SQLExceptions into DAOLayerException object and throw it back to Service layer, instead of doing the exception wrapping in every single DAO method, you can implement the exception wrapping in an After throwing advice and it’s done.
  • Logging and Profiling – off course, how can I leave these two? The evergreen examples(!) for the AOP.

I understand that this list is not an extensive one. So, if you have any more use cases to add this list, please leave a comment.

{ 15 comments… read them below or add one }

Vasil January 14, 2010 at 2:04 am

Another powerful use-case is bean validation. I mentioned this approach in my article http://vasilrem.com/blog/software-development/the-second-life-of-aop/

Reply

Veera January 14, 2010 at 8:43 am

informative. thanks for sharing.

Reply

Vasil January 14, 2010 at 2:12 am

…, and caching, certainly :) And many, many more.

As for the comment regarding the small number of use cases – it’s just funny. AOP brings to the java world flexibility of functional languages (almost).

Reply

Veera January 14, 2010 at 5:45 am

yeah. there are lots of other use cases that I’m missing here. but above are the one that I had faced in my experience.

Reply

davidecr January 14, 2010 at 5:48 am

+ auditing (who, when, invoke X with YZW information (parameters))

Reply

Veera January 14, 2010 at 9:05 am

yes. that is one important use case. In fact, in my last project, we did something similar using Spring AOP and we were using Log4j MDC context to pass the ‘who’, ‘when’ data across the layers.

Reply

Ramnivas Laddad January 14, 2010 at 5:55 am

> Here, “Use cases” are nothing but the cross cutting concerns in your application.

That is the right way to look at AOP. If your application has crosscutting concerns, AOP is likely to help you write better modularized code. I find that in my applications, I use AOP that cannot be classified into a strict category other than “crosscutting business-specific functionality”. This is not unlike in the OOP world–we don’t talk about use cases, because we simply use when it makes sense to model a concept as an object.

Here are a few additional use cases:
- Policy enforcement, especially architecture and framework policies. I have, for example, been saved by Swing and EJB policy enforcement aspects numerous times when I used to use those technologies.
- Making objects more powerful by injecting additional behavior through static crosscutting. Spring Roo (http://www.springsource.org/roo), for example, heavily utilize this idea.
- Domain object dependency injection (Spring’s @Configurable support that is used extensively in Spring Roo).
- Facilitating testing–especially for the exceptional paths.
- Extending IDEs in a systematic and robust manner (AJDT, Scala IDE).
- Concurrency control.

Here is a longer treatment of this topic from my book (sorry for the plug): http://manning.com/laddad2/excerpt_perspectiveAOP.html

-Ramnivas

Reply

Veera January 14, 2010 at 9:07 am

thank you for such a detailed comment. It really shows your experience and knowledge in Aspect Oriented Programming.

Reply

dhavaln January 14, 2010 at 12:02 pm

I’ve made a small distributed eventing framework inspired from Seam Component Events with Spring AOP to multicast events as soon as a method is called having @RaiseEvent annotation above it.

class EventGenerator{
@Out
private Data data;

@RaiseEvent(“x-event-queue”)
public void processData(){
// process data
// set @Out field values
} // Spring AOP @Around will multicast the event after this method execution
}

It’s working really cool.

Reply

Veera January 14, 2010 at 12:45 pm

interesting!

Reply

dhavaln January 14, 2010 at 2:00 pm

Spring and Spring AOP gives enormous power to reduce repetitive development.

In one of my previous application i made a web services framework with Spring, Spring AOP and XFire (now Apache CXF) to transparently consuming and exposing web services with validation, logging, security and content translation (like “0″ from SAP is “OK” for our system).

I made couple of services interacting with SAP Pi for testing and my team made 7 more services with zero knowledge on web services.

The team can actually concentrate on business logic development rather exposing them as a web services, validation and other repetitive stuffs.

Reply

alex January 14, 2010 at 9:51 pm

Another example would be a notification system. Nowadays you get an email for almost anything that happens (new message, somebody comments on your pic/vid).

Why build that into the program when you can easily manage it with AOP. It’s very similar to the auditing idea.

Reply

Veera January 15, 2010 at 8:48 am

yes. Notification is a common use case for AOP.

Another technology that I’ve used often for notification is the Log4j SMTPAppender. Using this, whatever messages that I log in my program will be emailed to the admin.

Reply

Ednelson January 18, 2010 at 3:16 am

Genesis framework uses AOP to synchronize swing conponent model with the presentation model.

Reply

Veera January 18, 2010 at 6:15 am

hm. that’s an interesting use case.

Reply

Leave a Comment

Previous post:

Next post: