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).
<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):
Uncomment it, and change it to:
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.
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.;)