AspectJ In Action

Java Development News:

AspectJ In Action

By Dion Almaer

01 Aug 2003 | TheServerSide.com

AspectJ In Action Sample Chapters
Two Sample Chapters from AspectJ In Action are freely available for download:


Chapter 3 - AspectJ Syntax Basics

Chapter 10 - Authentication and Authorization

You can place an order for this book at Amazon.com or Manning.com .


Introduction

Aspect-Oriented Programming (AOP) is a hot topic at the moment. If you mention it to developers, you will probably get one of the following responses:

  • What is that? Never heard of AOP
  • Isn't that 10 years old from Xerox PARC or something?
  • Ahh cool, finally something to help with my cross cutting concerns. I can't wait to see this develop.

If we look at history it kind of looks like AOP is following other technologies like OOP. It was conceived a long time ago (in computer terms), has been worked on in academia, and is now popping up more and more in the real world, and the private sector.

Since the bulk of the development world knows little about AOP, but is starting to really get interested in this topic, they need good books, examples, tutorials, etc to help them grok it.

I have probably read every book about AOP that is out there, since I am a big fan. Although AspectJ in Action isn't the first book on AOP/AspectJ, it is one of the best. Let's look at the structure of the book:

    PART ONE:

  • Introduction to AOP
  • Introduction to AspectJ
  • Advanced AspectJ

    PART TWO:

  • Monitoring techniques
  • Policy enforcement
  • Optimization: pooling and caching

    PART THREE:

  • Design patterns and idioms
  • Implementing thread safety
  • Authentication and authorization
  • Transaction management
  • Implementing business rules
  • The next step

    APPENDICES:

  • AspectJ compiler
  • Understanding Ant integration

You can see from this list that there are distinct parts to the book, with each offering something totally different.


Part One: Introductions, AOP, and AspectJ

As you open the book, you will immediately be faced with the question "how real is AspectJ?". With new technologies it is always hard for the average developer to judge when they should jump in. Ramnivas tries to assert, up front, that this is valid technology RIGHT NOW. Throughout the book you see ways in which you can incrementally join in the AOP game (from just using it at development, to using it on test, to production).

A book on AOP has to start with the basics. I wasn't looking forward to reading through "yet another cross cutting concerns" chapter which uses logging as an example, but I was pleasantly surprised. It is hard to express some of the ideas of AOP, and it has been done well here, with a mixture of graphics to make you think, and informative text. Different pictures are used to show the concepts (e.g. code scattering) in different lights, so I think most newbie AOP people will be able to get a really good "a ha!" out of this.

Ramnivas also uses the technique of showing what the code looks like after you go through AOP weaving, which really tries to take the mystery, and magic out of it. Sure, as AOP developers you don't have to know what the weaver/compiler is doing, but it is a good tool to show the developer what is happening in a way that is easy to understand.

At the end of the AOP introduction he takes on some "myths and realities of AOP". For example, you often hear people say "With aspects, doesn't it make it harder to follow the execution path of a program?". The book says that this is true ... BUT this is a step in the right direction! We have also seen this happen when we moved to OOP. With polymorphism it is hard to know exactly what object is being called upon, but we are OK with that now aren't we?

When we get into AspectJ, we are taught the basics, and get to see the typical "Hello World" program that we are used to seeing in all programming books. Again, we are shown what has happened "under the hood". The code of the hello world app AFTER the weaving is shown, which again gives us a feeling that this isn't all that weird and strange.

I find explaining join points to be interesting. What is the difference between call and execution? These questions are answered in style as we delve into AOP. The sequence diagrams give you the visual feel for what is going on. Then we get taken down the path of learning a lot about AspectJ pointcuts: within(), cflow(), this(), target(), args(), if(), etc.

Yet again, a good job of explaining advice (before(), after(), around()) is done, getting into details of returning, throwing, and proceed(). Good examples are used along the way, so you don't feel like you are learning pure theory ... rather "You will want to do something like X, and this is the construct you need to get to that".

After discussing the dynamic cross cutting (before() and co), we get into the static cross cutting that AspectJ gives you. We get taken down the path of understanding how we can add members, modify the class hierarchy, introduce compile-time errors and more.


Advanced AspectJ

The advanced AspectJ chapter talks about the reflective way that we can get information on join points (thisJoinPoint, thisJoinPointStaticPart, and co). I really enjoyed the talk on precedence, which is an interesting issue in AOP. When more than one matches happen on a joinpoint, what is the order? Many books quickly mention declare precedence : X, Y; but this goes into a bit more detail. I really like the best practice of creating a separate aspect that holds the precedence statement. By having this separated, two aspects are no longer coupled, and there is less of a chance that a clash of multiple precedence declarations can happen.

Another good topic is aspect association. By default, aspects are sort of like singletons; however, you don't always want that behaviour. This is where perthis(), percflow(), and friends come in.


Part Two: Basic applications of AspectJ

This is where we get really practical, and the book gets even better. If you already know AOP/AspectJ well, then you may well have skimmed the first part to get to this. We really need more practical guidance with AOP. In the rest of the book we get some of this much needed content. We do start off reading about logging, which has been beaten to death a little, and I do groan when I see it. The problem is that:

       (a) everyone uses the logging examples
       (b) people are starting to think "is AOP just for logging?"

However, to be fair, just because it has been talked about a lot, it shouldn't detract from the point that logging is still an important cross-cutting concern, and Ramnivas goes further than simply adding System.out.println() statements via AOP, and gets more into tracing and profiling of code.

Policy enforcement is a really interesting chapter. This is an example of using AOP in your project without having to put it into production yet. We are shown policy enforcement patterns, and then examples of restricting code to: EJB standards, and Swing policies. This is an interesting field, and would be especially useful on large projects, and in large organizations.

AOP is a natural for pooling and caching. We get a good taste of that with examples of creating an extensible resource pool, with database connections, and thread pooling added. We also get to see an XSLT stylesheet caching example. These are great, real world examples, and this is code that you could basically take and use with some minor tweaks. Ramnivas does a good job of showing how to create abstract aspects which are generic, and then using concrete aspects which inherit from the abstract ones, to give you the specifics of your use case.


Part Three: Advanced applications of AspectJ

Here we get into some interesting design patterns. We are shown how design patterns that we know and love from OOP, can be cleaned up even more by using some AOP. Here we get to see the Worker Object Creation pattern.

When you read the wormhole pattern you may get blown away at first. Wormholes in programming? You can pass context to methods without really passing context around? This is a really fun one to learn about (showing that AOP really isn't just interception!), and you may want to re-read that pattern a couple of times to get it. :)

Another really interesting idea is the "idiom" of creating interfaces that actually have implementation in them. That is right, you can do that in AOP. We are shown how, and why you would want to do that (it is REALLY handy), and the rest of the idiom section gives us good tips that other less practical books may not talk about:

  • Avoiding infinite recursion (you WILL run into this when you start doing AOP)
  • Nullifying advice
  • Providing empty pointcut definitions

Now things get more and more fun, as we walk through good practical examples such as:

  • Implementing thread safety
    • Ensuring Swing's thread safety
    • Improve responsiveness in UI apps
    • Implementing a read-write lock pattern
  • Authentication and Authorization
    • Using/monitoring JAAS via AOP
  • Transaction management
    • AspectJ based TX management using JDBC and JTA
  • Implementing business rules
    • Using AspectJ to modularize implementations of business rules
    • Using plain Java to implement rules
    • Using a rule engine with AspectJ

    This is a great chapter to see some real practical code in use, and to get a feel for what AOP can really do for you in your projects.


    Conclusion

    I would highly recommend AspectJ in Action for anyone who is interested in AOP. Although the examples all use AspectJ, I don't think you have to be an AspectJ user to get something from this book. However, if you are an AspectJ user, you will be able to run with the code examples used, and can play along. :)

    I liked the practical angle, and you could tell that Ramnivas has really used AOP/AspectJ on his projects ... he just sounds real world. We need more and more of this content, as we find design patterns and best practices in this new world. I am wary of people jumping into the new paradigm and trying to do everything as an aspect, so it will be interesting to see where we go with this.


    About the author of this review

    Dion Almaer (dion@middleware-company.com) is a Principal Technologist for The Middleware Company (www.middleware-company.com), one of the nation's leading training companies in EJB/J2EE and B2B technology training. TheServerSide.Com J2EE Community is a service of The Middleware Company.