Passing Properties To ColdSpring

Word Count: 365

I am sure most of you out there already know this but I did not. First off if you are not familiar with ColdSpring I suggest you head over to http://www.coldspringframework.org/ and learn more about it.If you need to pass dynamic variables into ColdSpring's xml file it turns out its really easy to do. The constructor for the DefaultXMLBeanFactory() method accepts two arguments one of which is defaultProperties. Here is what the init method looks like.

<cffunction name="init" access="public" returntype="coldspring.beans.DefaultXmlBeanFactory" output="false"
         hint="Constuctor. Creates a beanFactory">

   <cfargument name="defaultAttributes" type="struct" required="false" default="#structnew()#" hint="default behaviors for undefined bean attributes"/>
   <cfargument name="defaultProperties" type="struct" required="false" default="#structnew()#" hint="any default properties, which can be refernced via ${key} in your bean definitions"/>
   
   <!--- set defaults passed into constructor --->
   <cfset setDefaultAttributes(arguments.defaultAttributes)/>
   <cfset setDefaultProperties(arguments.defaultProperties)/>
   
   <cfreturn this />
</cffunction>

As you can see this method takes two arguments, one of which is a structure to define our properties. If we create a structure with properties, such as our dsn we can simply pass that to the constructor. The example below is setting up ColdSpring using the OnRequestStart method of my application component.

<cffunction name="onRequestStart" returnType="boolean" output="true">
   <cfargument name="thePage" type="string" required="true">
   
   <cfif NOT structKeyExists(application,"beanFactory") OR isDefined("url.reload")>
      <cfset csprops = structNew()>
      <cfset csprops.dsn = "mycart">
      <cfset application.beanFactory = createObject("component","coldspring.beans.DefaultXmlBeanFactory").init(structNew(),csprops)/>   
      <cfset application.beanFactory.loadBeansFromXmlFile("#expandPath('.')#\config\beans.xml",true)/>   
   </cfif>

   <cfreturn true>
</cffunction>

Finally we can reference the dynamic variables that we passed to the constructor in our config file.

<?xml version="1.0" encoding="utf-8"?>
<beans>
   <!-- product -->
   <bean id="ProductDAO" class="mycart.catalog.ProductDAO">
      <constructor-arg name="dsn"><value>${dsn}</value></constructor-arg>
   </bean>
   <bean id="ProductGateway" class="mycart.catalog.ProductGateway">
      <constructor-arg name="dsn"><value>${dsn}</value></constructor-arg>
   </bean>
   <bean id="ProductService" class="mycart.catalog.ProductService">
      <constructor-arg name="ProductDAO">
         <ref bean="ProductDAO"/>
      </constructor-arg>
      <constructor-arg name="ProductGateway">
         <ref bean="ProductGateway"/>
      </constructor-arg>
   </bean>
   
   <!-- product attribute -->
   <bean id="ProductAttributeDAO" class="mycart.catalog.ProductAttributeDAO">
      <constructor-arg name="dsn">
         <value>${dsn}</value>
      </constructor-arg>
   </bean>
   <bean id="ProductAttributeGateway" class="mycart.catalog.ProductAttributeGateway">
      <constructor-arg name="dsn">
         <value>${dsn}</value>
      </constructor-arg>
   </bean>
   <bean id="ProductAttributeService" class="mycart.catalog.ProductAttributeService">
      <constructor-arg name="ProductAttributeDAO">
         <ref bean="ProductAttributeDAO"/>
      </constructor-arg>
      <constructor-arg name="ProductAttributeGateway">
         <ref bean="ProductAttributeGateway"/>
      </constructor-arg>
   </bean>
   
   <!-- udf library -->
   <bean id="UDFLib" class="mycart.UDFLib" />
   
</beans>


Comments



Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.