LightWire Configuration Using ColdSpring XML

2007 April 10
by Paul Marcotte
As a follow-up to Peter Bell's "LightWire or ColdSpring?" entry, I'm happy to announce that LightWire now supports XML configuration using ColdSrping XML bean definitions. With the current release (0.65), you can set your object dependencies with xml, programatically, or a mix of both. This feature supports recursion, factories and optional default settings...Here are a couple of code blocks describing the objects used to manage a ficticious "Product" (complete example available in /LightWireXMLTest directory available via svn, or included in the zip). <bean id="ProductService" class="lightwire.LightWireXMLTest.com.model.product.ProductService">
   <constructor-arg name="ProdDAO">
      <ref bean="ProdDAO"/>
   </constructor-arg>
   ...
   <property name="CategoryService">
      <ref bean="CategoryService"/>
   </property>
   <property name="MySetterTitle">
      <value>My Setter Title Goes Here</value>
   </property>
</bean>
<bean id="ProdDAO" class="lightwire.LightWireXMLTest.com.model.product.ProductDAO">
   <constructor-arg name="dsn"><value>${dsn}</value></constructor-arg>
</bean>
<bean id="Product" class="lightwire.LightWireXMLTest.com.model.product.ProductBean" singleton="false">
   <constructor-arg name="ProdDAO">
      <ref bean="ProdDAO"/>
   </constructor-arg>
</bean>
In this example (from beandefs.xml.cfm), ProductService has both constructor and setter injected dependencies. Product, as a transient object, has ProdDAO (alias for ProductDAO) injected into it. The default setting placeholder ${dsn}, will be replaced by the value of key "dsn" in the settings struct example below: <cfscript>
// OPTIONAL: TO pass default values for configuration settings, create a struct

var settings = structNew();
// Call the base init() method to set sensible defaults. Do NOT remove this.

Super.init();
// OPTIONAL: Set lazy loading: true or false. If true, Singletons will only be created when requested. If false, they will all be created when LightWire is first initialized. Default if you don't set: LazyLoad = true.

setLazyLoad("false");
// provide a default value for dsn

settings.dsn = "dsn_name";
// parse xml bean definitions using default settings

parseXMLConfigFile("#expandPath('.')#/beandefs.xml.cfm",settings);
// set mixins programatically

addMixinProperty("ProductService","MyMixinTitle","My Mixin Title Goes Here");
addMixinProperty("ProductService","AnotherMixinProperty","My Other Mixin Property is Here");
addMixinDependency("ProductService", "CategoryService");
</cfscript>
The above code is from BeanConfig.cfc. Here I created a struct to pass the value of my dsn to ProductDAO. Mixins, which I have never used (yet), are available in LightWire only, so the last three lines are an exmple of the native LightWire programatic config. To intialize and configure LightWire you use the follwing code (assuming you have the lightwire directory in your web root) in your app: <cfset myBeanConfig = createObject("component","lightwire.lightwirexmltest.BeanConfig").init()>
<cfset myBeanFactory = createObject("component","lightwire.LightWire").init(myBeanConfig)>
To intialize and configure ColdSpring, you would set the defaults settings in the same file you instantiate ColdSpring: <!--- optional settings --->
<cfset var settings = structNew() />
<cfset settings.dsn = "dsn_name" />
<!--- create coldspring beanFactory--->
<cfset myBeanFactory = createObject("component","coldspring.beans.DefaultXmlBeanFactory").init(structNew(),settings) />
<!--- load bean definitions --->
<cfset myBeanFactory.loadBeansFromXmlFile("#expandPath('.')#/beandefs.xml.cfm",true)/>
If you like to configure your applications using xml, you will now be able to use either ColdSpring or LightWire (with the exception of programtic Mixins or the or tags in ColdSpring). Future releases will likely support and ColdSpring DTD validation. To learn more about ColdSpring and the whys and hows of Dependency Injection, I suggest reading ColdSpring articles by Brian Kotek. For more information on LightWire, go to the source.