Wednesday, March 05, 2008

The Gist of Spring Framework

I've avoided the Spring Framework for some time now. Mostly because I found the XML configuration a little daunting. I could see how nicely the little Hello World examples they gave could work, but the thought of using that on any real-world application seemed impractical. I don't know about you, but I *hate*, wait, let me rephrase that - *HATE* - loads of XML configuration. A few years back it became the trend-du-jour to slap all your configuration into XML files, away from properties files. There was some benefit, since XML files are multi-dimensional in their logical structure.

Anyway, years later, and I hate XML configuration. And Spring seemed to me to be the abomination of all XML configuration tabernacles. I've always kept my eye on it, since everybody and his grandmother seemed to be using it and raving about it. So right now I'm working on a web application that incorporates a few services, including web services, and I figured that since I'd noticed Springs configuration has improved a lot with the use of annotations and autowiring, I'd give Spring a whirl.

I'll probably comment more about my experience with Spring as time goes by, but for now I wanted to make one comment to those fellow Spring newbies out there who might be puzzling with a thing or two. What I call, the 'gist' of Spring. And this is it:

The application context replaces the use of the 'new' keyword.

So, what does that mean? Obviously you still use the 'new' keyword, but the one thing that I couldn't quite get, was when they gave examples of classes that were so wonderfully injected and aspected by Spring, they never showed exactly how to instantiate instances of that class, the one being worked over by Spring. And objects (or 'beans', the word I wish had never entered Java vocabulary) created out of Spring all seemed to be geared toward a Singleton approach. In other words you created an EmailService that all objects used, that needed it of course.

Well this couldn't be further from the truth. Truth be told, you can create any form of object from your Spring application context. And that is the key- the 'gist'. When you need an object, that you want to be setup for you by Spring, get a handle to the ApplicationContext, and use that to create your bean, and it will do all the rest for you. Any class, any object, so long as you've set it up correctly in your application context XML file(s), you're good to go. It's pretty sweet actually.

The next challenge I'm finding is how to get hold of the ApplicationContext. In my case it was fairly simple, in that I can get it from the servlet context for my web application. But you need to then keep a reference to it so you can actually use it in your code. I'm still figuring out the best way to do this. Obviously all the samples use a very simple local application context in the form of some or other test, which also doesn't really help me, but I suppose my frustrations are mostly due to my ignorance. Again though Spring developers, it wouldn't hurt to also show the call in your examples that shows how the actual bean we're after gets created. In other words "ctx.getBean("myBean");". Look at your examples and you'll see. You show the class annotated up to the wazoo, but you don't show how that class is instantiated. Probably because it's trivial to you, but not to us newbies.