Thursday, June 25, 2009

Getting Started with Spring 3.0 AOP

Choosing the latest-and-greatest of various technologies may serve you well, and it may also give you some headaches. For projects with longer timelines, it's useful to deploy your first GA with up-to-date infrastructure; but in spinning things up, you're subject to early-adopter pain. Here's my recent experience with Spring 3.0.0, in particular the AOP component of that framework, shortly after that version's first release. I'll assume you've already downloaded the 3.0.0 distribution and are motivated to test-drive Spring IoC and AOP functionality.

My prototyping began of course with basic Spring Core. Since, at the time of this writing, the Spring 3.x stuff is not in Spring's repository, I couldn't simply declare the org.springframework.core jar in an Ivy file and let Ivy do the dependency resolving for me; this is the first point of pain we're bumping into (and I'm sure there'll be more). Here's how I addressed this.

A very simple program that leverages Spring IoC required these jars (which I discovered attempting to run the program and simply observing what classes needed loading, and making some decent guesses as to what Spring distro jars would provide them) :

org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3

Testing my IoC program revealed additional non-Spring class-defs-not found, and long story short I added these:

antlr-3.0.1
commons-logging

With these in place, my IoC prototype ran just fine. Other dependencies may be called for if a more sophisticated program is used; but my intent here is not to itemize the exhaustive possibilities, rather it is to document how I went about figuring things out for your (and my) future reference.

From here, I wanted to do some proof-of-concept with the AOP stuff. From my read of the Spring developer guide, I concluded I want to use Spring AOP (vs. AspectJ), and I want to use the @AspectJ annotation-style approach. I won't go into my rationale here; that's a different topic. As our project unfolds, I'll provide more insights in future posts.

To use the AspectJ annotation-style, I began by placing the following into my Spring XML configuration file:

In the beans tag, add this attibute to declare the aop namespace:

xmlns:aop="http://www.springframework.org/schema/aop"

...and at the end of the beans tag body, add this declaration:

<aop:aspectj-autoproxy/>

Now, I run my program again; note that I've added no advise or pointcuts yet - I only want to get off the ground in establishing my dependencies, of which I'm sure some are now missing. Sure enough:
ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor
Now, this one is obviously not a Spring Framework class; it's a third-party. I could probably use any repository to retrieve it, but I choose to use Spring's repository just to stay consistent. That repo is located at http://www.springsource.com/repository/app/. Interestingly enough, note that the trailing slash is a required part of this URL - my guess is that this is a RESTfully-constructed website in which the trailing slash indicates a collection, and without which the URL is not a valid identifier. But REST is, again, a different topic.

Searching for the missing class in Spring's repo, it offers me an Ivy entry, which I add to my ivy.xml and run the ivy:retrieve ant target. Here I'm assuming you have familiarity with Ant and Ivy, and know how to configure a simple Ivy environment; to digress on these topics here would make this post much too long, but I want to circle back eventually with a post on Ivy.

Running the Ivy retrieve task fetches the AOP Alliance library for me, and upon adding that to my project build, there is one more missing reference to an AspectJ class; I fetch this one using the same technique as above, choosing the 1.6.5. RELEASE version of AspectJ-Weaver. I rebuild with this in place, and re-run my program - this time, without any problems.

At this point, my goal is accomplished; I only wanted to get "off the ground" with Spring AOP. Now it's time to dig a little deeper and see what the AOP stuff has to offer.

No comments:

Post a Comment