Splitting up your ColdSpring.xml file in a Model-Glue Application

While I’ve used Model-Glue for many years now, I’ve only recently begun to seriously drink the ColdSpring kool-aid. The more I use ColdSpring, the more I appreciate how well it works with Model-Glue. I’m writing up some recommendations for Model-Glue application design, and one of the things that occurred to me is that while I typically recommend breaking up the ModelGlue.xml file into multiple files, it might also make sense to break up the ColdSpring.xml file. Right now this file contains the settings for your Model-Glue application (reload, debug, etc) and any custom beans you want defined.

So I spoke with Scott Stroz about this and he clued me in to the fact that this is actually already possible! So big thanks to him for teaching me this tip, and hopefully it will help others as well. To begin, create your custom beans.xml file. I’d keep it in config with the rest of the config files. Remember, this is the file that will store all the beans for your application. Your ColdSpring.xml file will only be used to configure the Model-Glue settings for the application.

Next, you create a new instance of ColdSpring and store it within the Application scope. I modified the Model-Glue default Application.cfc like so:

<cffunction name="onApplicationStart" output="false"> <cfset application.cs = createObject("component", "coldspring.beans.DefaultXmlBeanFactory").init()> <cfset application.cs.loadBeansFromXmlFile("/mgtest/config/beans.xml", true)> </cffunction>

Ok, one last step. When it comes to working with ColdSpring, there is a setting you can override in the main index.cfm file:

<!--- **HIERARCHIAL BEAN FACTORY SUPPORT**

If you'd like to designate a parent bean factory for the one that powers Model-Glue,
simply do whatever you need to do to set the following value to the parent bean factory 
instance:

&lt;cfset ModelGlue_PARENT_BEAN_FACTORY = ??? /&gt; ---&gt; </code>

I never really paid attention to this before, but this is what we need to change. Simply add:

<cfset ModelGlue_PARENT_BEAN_FACTORY = application.cs />

And bam - you’re done. My biggest concern was that this wouldn’t be as “tied” as when you just use ColdSpring.xml. However, that proved to be completely incorrect. I was able to define beans within my controller just fine:

<cfcomponent output="false" hint="I am a Model-Glue controller." extends="ModelGlue.gesture.controller.Controller" beans="userService">

And then use it within a controller mehtod:

<cfset event.setValue("message", beans.userService.helloWorld())>

Woot. Thanks Scott, thanks Model-Glue, and thanks ColdSpring.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments