Spring Awakening – Spring 3.1
My wife and I saw the musical on Broadway a few years back, but of course I’m talking about a different Spring. Last summer I played around with Spring and Fitbit’s APIs for integration of their device into some apps I was working with. I’ve started to play around with Spring again. There’s a new version of the framework and tools. Part of the new tools is a preview of Eclipse 4.2 – my goal here is to check out what’s new in spring, play around with the MVC changes, take a dive into the WebFlow and see what’s changed with the web service API support – there’s some new things happening with REST style interfaces.That’s a whoel lot – so let’s get going.
Note: I’m going to publish this incrementally since it will probably be a large post that takes me a few weeks to put together.
I’ll be working on Windows 7 Ultimate as a base OS with Java 1.6 and Tomcat 7. I’ll probably be using Eclipse Indigo and Spring Tool suite (STS) to see how they differ and which I prefer for spring development. For the most part I’ll probably use the current released version (2.9) although it is very tempting to use the M1 of the next release that also includes Eclipse 4.2. Yummy 🙂
Spring 3.1 – Re-learning What I had Learned Plus a Bit More
I’m no super expert with Spring so it takes me some time to get back into it. Where are good spots to pick up Spring? Here’s a few decent spots I’ve found useful:
- Spring Docs – basic documentation
- Spring Tutorials – video and blog tutorials from the Spring community. The audio on the video tutorials is just terrible. Video tutorials suck anyway – I would much rather read something quickly. The written tutorials are pretty sparse.
- Maven and Spring – how to use maven with Spring
- Getting Started with Spring MVC – a quick introduction to a basic MVC web app.
- Spring MVC Showcase – some excellent stuff to understand the basics of MVC pattern web apps with Spring
- Vaan Nilla – decent quick tutorials, but not always the latest version of Spring.
Of course there are a bunch of books on Amazon and then there’s courses from SpringSource. I picked out a book too: Pro Spring 3 (Apress). Its surprising that there aren’t a few more decent sites with quickie tutorials. This is a hefty book – nearly 1000 pages. There’s a lot of source code included as examples. The book seems pretty straightforward and so far has a sensible approach (I’m only in chapter 3).
Starting Out – Tools and the Classic “Hello World”
Spring Tools Suite 2.9.x seems to be the way to go right now, I’ll explore other options later. In STS when you make Spring projects, be sure to use File, New, and one of the Spring things – like Spring Project Template. Out of habit it is really easy to get hit Project and then you miss the goodies.Building the first app in the book was a bit tricky, but I got thru it. The book kinda cuts it off prior to finishing it and the sample code seems incomplete too. The rick was to finish off the renderer class, adding a property to the message provider class, flush out the renderer class, and then setting this property in the app context file. See the gallery below for some help.
I suppose it is arguable, but IMHO Inversion of Control and Dependency Injection is probably Spring’s most influential contribution to programming. I would try to describe these in some way that makes sense, but really – check out the books or Spring site. Essentially IoC and DI are twists on how to lookup and use objects. Study hard – you really need to get it to get the most out of Spring.
The various techniques to use and then populate beans with configurations in XML files or annotations is pretty darn cool I think. One tip I learned from looking at the examples from the Spring folks and in my book is about the path for storing and looking up these files. Check out a basic project structure created by the Spring project template for a utility project. At the top of the structure are the package folders for source code and configuration information. Configuration goes into src/main/resources and code obviously goes into src/main/java.
You can add packages (folders) to the resources package – Spring will place default template files under META-INF/spring. To reference these files in code use a line like this:
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
Not rocket science, but these practical details seem to always get skipped over in tutorials, books, and guides.
More on Configuration with Spring
After messing around with bean initialization and configuration is seems that using the InitalizingBean interface for bean configuration is the best of three potential options. Although it does sorta tie you into Spring, but to me I’ve already made that commitment. With this interface you just replace the use of an init() method with the interface required method afterPropertiesSet() method. You keep on using the XML configuration file. Then if your beans need to clean up you can use DisposableBean (another interface) to help out with that. Everything is consistent and easy to understand 6 months later.
While checking out bean configuration, factory beans and singleton use is another area to check out and understand. These will be important for understanding how to access JNDI/JPA resources for directory services and databases. The Pro Spring 3 book does a pretty good job explaining factory objects and using XML configuration objects in Chapter 5. I can definitely see the power of the DI/IOC container, however I can also see that it will require a lot of time and practice to really become proficient with the XML configuration parameters and possibilities for what can be done and how to do it.