Saturday, November 28, 2009

Spring - the silver bullet

Last week I was interviewing couple of Java Techleads/architects from the industry. The fact that almost everybody has used Spring in most of their projects didn't come as a surprise. I remember my life as an J2EE developer many years ago. I remember we spending half the time fighting container complexities over coding business functions (Here me expressing my gratitude to EJB3 efforts back in 2005).
Still were all faults were with J2EE spec?  I agree that the EJB spec had enough pitfalls itself, but the core of the problem was more to do with the way it was used. Any technology has a place and EJB2 was no exception. It was really useful in certain challenges such as dealing with distributed databases, component distribution or in message driven applications. But unfortunately it was accepted and promoted as the de-facto blueprint for all enterprise Java applications by the giants at that time. For that reason many of the developers selected EJB to write every JEE application without much rationalizing. 
When I read Rod Johnson's second book 'J2EE development without EJB - 2004' five years ago, Spring looked appealing with the simplicity it offered. Although I never considered Spring as an alternative to EJB, it was definitely a better choice for people who just "depends" on blueprints for architecting. I agree that Spring way is better than EJB way, but does that mean it is the way forward? I used Spring for some work some years ago but I was not completely happy about it. This does not mean the principles behind Spring were bad but Spring philosophy of being a one-stop-shop for JEE distracted me a lot. Spring started to become a complex day-by-day and now it can no more be considered a light weight framework. 
Recently I revisited Rod's original Spring book "J2EE Design and Development - 2003". Here are some picks from the 'conclusion' chapter. Interestingly, the arguments Rod brings against EJB appears to be very relevant to Spring use today.
Rod: A good application is the simplest possible application
>> Don't many of us use Spring quite blindly? I have seen many techies promoting all important objects to be Spring beans and the injections to be via XML configurations. Who can really consider this to be the simplest possible application with your business logic divided in to a thousand line XML file?
Rod: Don't use EJB unless it delivers real value
>> Very same applies for Spring as well. Back on the interviews... I asked the interviewees to explain me when to use Spring beans and when to use plain Java bindings. I Hardly got an answer from any of them. Just because your framework allows you to bind at runtime, should you do that unless needed? 
My last Java project was to build a configurable search engine. I decided not to use Spring although project had much to share. I made my POJOs testable with plain setter or constructor injection patterns. It cost me writing additional constructors and setter injectors for mocking but ultimately proven to be simpler to Spring configurations. There were some instances it was really necessary to inject dependencies at runtime where I used simple reflection based plugin mechanism to inject dependencies (but this is only in handful of situations). So Don't use 'Spring' unless it delivers real value.
Rod: Try to achieve portability between J2EE application servers if
>> Spring books demote using Spring at its lowest denominator but promote making use of as much as framework features to 'empower' your application. Soon developers may find them building application tightly coupled to Spring and following an architecture laid by somebody else having no idea of your requirements. When time come to revert, Spring might have consumed you beyond rectification. So Try to achieve portability between any framework you use including Spring. 
Rod: Use J2EE: don't let it use you
>> My message is that be conscious of what you do even with Spring. Do not just follow blueprints unless your business requirements justify them. Use Spring: don't let it use you. 

Please do not interpret this post as that I do not like Spring. I love the innovations Spring folks bring in with every major release. I love using Spring as a platform when appropriate and its appropriate more often than some other platforms. It is just that I'm disturbed to see J2EE mistakes are being repeated again with Spring. 
Everything works at its best when used, not when overused!

[tag: 99xt ]