Dynamic Properties in the ColdSpring Configuration File

A powerful but often underused capability of ColdSpring lies in the ability to specify dynamic values to use in your XML configuration file. Let's demonstrate how we might use this capability.

You may recall from the constructor argument discussion that our ConfigBean is being passed a datasource name. In that previous example, we were hardcoding a value of "myDSN" into the XML for this value, like this:

<bean id="configBean" class="coldspring.examples.quickstart.components.ConfigBean">
	<constructor-arg name="datasourceName">
		<value>myDSN</value>
	</constructor-arg>
</bean>

To put it bluntly, that kind of sucks. Hardcoded values like that should be one of the first things to set off warning lights in your head. What if we want to easily reuse this configuration file in more than one application? Where the applications need different datasource names? Luckily, there's an easy solution. We can specify a special placeholder value in the XML to tell ColdSpring that this is a dynamic value:

<bean id="configBean" class="coldspring.examples.quickstart.components.ConfigBean">
	<constructor-arg name="datasourceName">
		<value>${dsnName}</value>
	</constructor-arg>
</bean>

Note the special value with the dollar sign and the brackets. That's the placeholder. Now, when we create our ColdSpring factory, we can pass in a structure that defines what we want to use for those dynamic values. This would look like:

<cfset coldspringConfig = '/coldspring/examples/quickstart/config/coldspring.xml' />
<cfset properties = StructNew() />
<cfset properties.dsnName = "myDSN" />
<cfset beanFactory = CreateObject('component', 'coldspring.beans.DefaultXmlBeanFactory').init(defaultProperties=properties) />
<cfset beanFactory.loadBeans(coldspringConfig) />

As you can see, our structure has a key named "dsnName" and a value of "myDSN". When we give this to ColdSpring as a constructor argument, it automatically will replace the matching placeholder ${myDSN} with the corresponding value in our properties structure. This can lead to much more flexible configuration files.

Options for Defining Dynamic Properties

You might want to save this for later investigation, but there are some other nice ways to define the dynamic properties that you pass to ColdSpring beyond building a structure. When you feel up to it, have a look at the Environment CFC which is discussed in the ColdSpring extensions section.