LightWire Configuration Using ColdSpring XML

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.

3 responses so far ↓

Dan Wilson - Apr 11, 2007 at 1:43 PM

Very cool example Paul.

Keep 'em coming!

Dan Wilson

Fancy Bread - Apr 11, 2007 at 3:25 PM

Thanks, Dan. There's a lot more to write about. Expect some use cases and perhaps a design pattern example or two in the future!

Paul

Redskins Jersey - Sep 21, 2011 at 4:20 AM

Fabregas free running, running, and he used his tactical awareness of a great help for us. Coach Guardiola said. Among the melon handsome imagination, Washington Redskins Jerseys Albert Haynesworth Jersey Brian Orakpo Jersey Chris Cooley Jersey Chris Horton Jersey Fabregas will play more events on the pitch middle midfielder role. During the Arsenal, Fabregas repeated muscle injuries. But in fact, Washington Redskins Jersey Clinton Portis Jersey Darrell Green Jersey DeAngelo Hall Jersey Devin Thomas Jersey he and Lionel Messi, as has a very strong muscles, so to adapt to the Barcelona game no problem. This makes Guardiola was very satisfied. His goal should be to go beyond the resume Harvey. Redskins Jersey Dexter Manley Jersey Mike Sellers Jersey Sammy Baugh Jersey Tim Hightower Jersey Guardiola said. Back to Barcelona, abregas is happy.

Leave a Comment

Leave this field empty: