Apr 15 2008

Sharing MG ColdSpring Beans With Other Applications

Posted by Joe Rinehart at 3:09 PM
3 comments
- Categories: Model-Glue

When you write a Model-Glue application, it both manages ColdSpring for you and gives you a place to define beans.

Eventually, though, you may want to use some of these beans outside of your Model-Glue application but still inside the same application scope. You may be writing a Flex application or simply creating a common pool of beans / services all of your apps can share.

To do this well, you'll need to use what are known as "parent bean factories." Simply put, you'll first create your own instance of ColdSpring's bean factory then instruct your Model-Glue application to look for beans first in its internal and then in this "overaching" ColdSpring factory.

Step 1: Managing ColdSpring yourself

You'll first need to manage creation of your own instance of a ColdSpring bean factory. In your Application.cfm (easy to port to Application.cfc), you'll want to add code to create it or reload it as necessary (Note: this can be improved, and should be, with a double-checked lock).

<!--- Reload beans if not present or url.init is defined --->
<cfif not structKeyExists(application, "sharedBeanFactory") or structKeyExists(url, "init")>
<cfset application.sharedBeanFactory = createObject("component", "coldspring.beans.DefaultXmlBeanFactory").init() />
<!--- Load your common beans XML file --->
<cfset application.sharedBeanFactory.loadBeans(expandPath("/my/shared/beans.xml")) />
</cfif>

Ok. Your application now contains a DefaultXMLBeanFactory instance. You can test it out by writing a quick index.cfm with an application.sharedBeanFactory.getBean("myBean") call that'll return a bean from the XML file.

Step 2: Configuring Model-Glue

With that taken care of, we need to tell our Model-Glue application to look first in its local config/ColdSpring.xml file first, then to look in the sharedBeanFactory second.

Open up your Model-Glue application's index.cfm, and you'll see this line of code commented out (assuming it's from an MG2 application template):

<cfset ModelGlue_PARENT_BEAN_FACTORY = ??? />

Uncomment it, and change it to:

<cfset ModelGlue_PARENT_BEAN_FACTORY = application.sharedBeanFactory />

Ok, you're good to go! Beans defined in the sharedBeanFactory's XML file are now available to your Model-Glue application as well as any other applications in the same application scope.

Comments

Raymond Camden

Raymond Camden wrote on 04/15/08 4:18 PM

Thanks Joe, that worked like a charm. You know - I think this whole 'Model-Glue' thing might be kind of cool.

;)
Shimju

Shimju wrote on 07/24/08 12:41 PM

This feature is cool Joe!
Henry Ho

Henry Ho wrote on 08/27/08 5:18 PM

I found out this method you posted somewhere in the MG newsgroup, but this method is not perfect.

For instance, I need to call ?init=true almost everytime I changed something in the model and/or controller because by using ModelGlue_PARENT_BEAN_FACTORY, somehow the model/controller cfc singleton is cached but never get updated like before. Anyone else having the same problem? Is there a workaround?

Write your comment



(it will not be displayed)