Wednesday November 29, 2006
Simplifying Spring Configuration
I meant to write a longer post about this topic, but as some opinion leaders are blogging about it at the moment, I’m also adding my 2 cents to the whole Spring configuration issue and tell you a little bit about XBean (my favourite configuration mechanism).
Before one decides which IoC configuration option to go, I think it is important to look at the target group. Who will create the configuration files? If your “users” have a development background, there are not many limitations - only keep in mind that most developers are lazy.
So almost everything is better than the verbose Spring XML syntax as we know it.
If you users are more “endish” users (e.g. I think of operations people) it is very, very important to have a simple, concise and meaningful syntax. In that case, the current Spring syntax isn’t a good fit, but also the latest developments using the “p” namespace does confuse users because of using namespaces and naming conventions such as the “-ref” suffix to reference other beans (more details at Rod Johnson’s blog: “XML Syntax Sugar in Spring 2.0“).
So, what am I proposing? Well, for quite a while XBean (part of the Apache Geronimo project) has been an option, which provides functionality to configure Spring beans using a vanilla XML file.
An example: Something like this…
<beans>
<bean name="myBean" class="com.example.MyBeanClass">
<property name="greeting">
<value>Hello, World!</value>
</property>
<property name="recipients">
<list>
<value>jon@doe.com</value>
<value>scott@tiger.net</value>
</list>
</property>
</bean>
</beans>
… will turn into this …
<beans xmlns="http://example.com/namespace" >
<helloWorldConfig>
<greeting>Wazzup, World!</greeting>
<recipients>
<recipient>jon@doe.com</recipient>
<recipient>scott@tiger.net</recipient>
</recipients>
</helloWorldConfig>
</beans>
Easy, eh? I’m not going into detail here, but XBean provides an extended ApplicationContext class to process these files. The XBean Application context uses a special properties file, which contains meta-information about how to map the XML onto the beans. Usually the properties file would go into the JAR file containing the class files.
XBean offers the option to annotate properties in the bean classes, which will be configured using the XBean configuration files, so that you don’t have to create the properties files by hand. These tags are also used to create a XML schema and a schema documentation - very neat.
The downside of XBean so far has been the lack of good documentation. Craig has a good post here and maybe I get to write a short tutorial about it some day.
Update: Rod notes, that Spring provides functionality to externalize single properties to config files. In case you have missed it: I have posted about this before.
Posted on Nov 29, 2006 at 22:55 (MET) | Permalink | 2 comments