Meet ColdSpringXMLUtils, a New Addition to the ColdSpring Bean Utilities Family

One thing that I've heard people ask about in the ColdSpring mailing list over the years relates to properties. These are quite useful. If you didn't know, you can specify a dynamic property in certain places in your ColdSpring XML using the syntax ${myPropertyName}. You then pass a structure of property names and values into ColdSpring, and it will substitute your property names for the placeholder values.

It's a nice feature, but it only works in specific places, like simple string values. People regularly discover this option, and then wonder why they can't put dynamic properties anywhere in the XML.

Well, now you can!

The ColdSpringXMLUtils component has joined the ColdSpring Bean Utilities RIAForge project. It will replace dynamic properties in a ColdSpring XML file with values specified in the passed structure. Using this CFC allows you to place dynamic properties anywhere in the XML. Note that because the ColdSpring XML file can be quite sizable in large applications, this component uses a Java StringBuilder to avoid using up large amounts of memory while doing the replacements. As a result, this component must be used on a server that is running Java 5 or later. Most installations of ColdFusion 7 or 8 should be running at least Java 5 so this should not affect many people. Usage of the ColdSpringXMLUtils is fairly straightforward. An original ColdSpring XML file might contain an element like this:

<bean id="userService" class="myapp.components.services.userService" />

Using this CFC, you could make part or all of the class name dynamic, like this:

<bean id="userService" class="${servicePackage}.userService" />

You would then define a data structure to map the dynamic properties to the values that you want:

<cfscript>
dynamicProperties = StructNew();
dynamicProperties.servicePackage = "myapp.components.services";
</cfscript>

Then simply use this CFC to create the bean factory like this:

<cfset coldSpringXMLUtils = CreateObject('component', 'path.to.ColdSpringXMLUtils').init() />
<cfset beanFactory = coldSpringXMLUtils.loadDynamicColdSpring('/myapp/config/coldspring.xml', dynamicProperties) />

This component will replace any matching dynamic properties in the XML, create the bean factory, and return it for you to use!

I've added this component to the zip file and SVN repository at RIAForge. I hope folks find it useful!

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
C'mon Brian, why aren't you just writing entire CS.xml for me? Make that component go through the directories in "servicePackage" and find the beans to list in the CS xml and write it out for you. Heck, it could even do a form of "auto-wiring" by reading each file, keeping a list of setters and matching that to other components and then write those xml property tags too.

There's your weekend homework assignment. :)
# Posted By Matt Williams | 3/21/08 11:01 AM
Actually that wouldn't be all that hard to do. The CFCStubGenerator already generates ColdSpring XML for you based on the UML and any composition/aggregation relationships. And the BeanInjector already loops through component metadata and finds matching beans. Maybe if I ever find time I'll give it a shot. ;-)
# Posted By Brian Kotek | 3/21/08 11:23 AM
Fantastic. Fantastic. Fantastic.
Thanks.
# Posted By John Allen | 3/21/08 11:24 AM
Do people really run CF 7 under JVM 1.5? I didn't that was supported and thought I read it would mess up Verity. Is this not the case?
# Posted By Kurt Wiersma | 3/21/08 11:48 AM
I'm really not sure, but I thought it did. I haven't used CF7 for a long time so I admit I'm guessing. Further, I should have noted that the component also uses ReMatch() which is a CF8 function. So to run this on CF7, Java 1.5 would be required but it would also require using an alternative method to find the matches in the XML.
# Posted By Brian Kotek | 3/21/08 12:18 PM
Very cool. Thanks
# Posted By Beth | 3/21/08 2:42 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9. Contact Blog Owner