Monday, June 28, 2010
Dynamic Spring Beans
Ever wonder how to dynamically change the class used for a Spring Bean at runtime without modifying your war file in JBoss? After lots of trial and error i put the two important pieces of this puzzle together. The first and probably most important part of the process is the use of a Static or instance bean factory. This handy little device allows for dynamic determination of a bean's class at startup time. The easiest thing to do is to create a static factory because it involves less beans. Now dynamically changing the bean at jboss startup time without changing the war took a little more research. To handle this it involves some JNDI bindings in the jboss-services.XML and the addition of a J2EE property bean in your spring file. The spring property bean loads the value from the jndi binding and then it can be passed to the bean factory. Voilà you can easily switch between implementations of an interface without a recompile and without changing your war file.