Sunday, March 29, 2009

I have worked with Spring for awhile on the side, as a pet project, and something just to try out. Not until recently, did I get a chance to use it in an "Enterprisy" environment. I didn't quite expect this discussion to ever come up, but I was genuinely impressed when a manager actually brought up the topic around the approach to spring bean configuration.

SpringSource recommends configuring Spring beans using the constructor method. This allow the beans to be immutable and immutable objects improves the orthogonality of the code.

The manager on the project said the company recommends bean configuration through properties. The reasoning she provide is improvable readability. This is true since reading the spring configuration of beans injected through constructor does not indicate which "property" is being set. However given the integration of today's IDE with spring, one can quickly look up the source code to determine the property being set via the constructor. If a third party library is being used, IDE these days are also tightly integrated with javadoc where you can quickly look up the parameter you are setting too.

Given all the advantage of immutable object and improve orthogonality of the code, I would strongly suggest this out weigh the "inconvenience" or "decrease in readability" of the configuration file. I am a big highly orthogonal code or system, but this is a topic all by itself for another post. For now see Eric Lippert's blog on orthogonality in the context of programming @ http://blogs.msdn.com/ericlippert/archive/2005/10/28/483905.aspx

Like all things in life, I do think constructor-injection is a rule of thumb. It should be by no means follow blindly. Constructor-injection is hard to read due to the nature of setting via either "Order" or "Type", once you get a certain amount of properties to set and a lot of them are of the same type, it gets really confusing. When you reach that point, it is important which we re-evaluate the original position: Is making the bean immutable worth the price to pay in readability?