Skip to content

How do I load on application start like variables in Mach II?

thofrey edited this page Apr 1, 2014 · 3 revisions

Table of Contents

  1. Addition to the machii.xml file
  2. Create appPropery.cfc in the property directory
  3. Use the variables in your app

If you are converting from a none frameworked app then you probably had an application.cfc or application.cfm that did code something along the lines of:

    <cffunction name="onApplicationStart" returnType="void" output="false"
        hint="Only runs when the App is started.">

        <cfset application.myVar1="Some Info">
        <cfset application.myVar2="Some Other Info">
    </cffunction>

Starting with Mach-II 1.5, we can use property.cfc and the above variables can be stored in the Property Manager. They can then be accessed as below:

    <cfset aVar=getProperty('myVar1')>
    <cfset setProperty('myVar2', aString)>

So how do we do this? There are several stages.

Addition to the machii.xml file.

You will need to add a property to the machii.xml file that points to a cfc that sets up your application variables.

    <properties>
        <property name="appProperty" type="pathToYourApp.property.appProperty" />
    </properties>

This points to a file called appProperty.cfc in the property directory of your application.

Create appPropery.cfc in the property directory

Create a cfc called appProperty in the property directory (you can call it anything you want but the machii.xml config file must be changed to reflect this).

This type of technique can be used to set platform or environment specific properties. This might be things that vary based on where the application is deployed (i.e. development, staging, production) such as DSNs, paths or debugging settings.

The cfc should look something along the lines of:

    <cfcomponent
        displayname="appProperty"
        output="false"
        extends="MachII.framework.Property"
        hint="My app property CFC for my app">

        <cffunction name="configure" access="public" output="false" returntype="void"
            hint="Called automatically by Mach-II on application initialization">
            <cfset loadApplicationVariables() />
        </cffunction>

        <cffunction name="loadApplicationVariables" access="public" output="false" returntype="void"
            hint="Some method to load you application variables">

            <cfset setProperty('myVar1',aString)>
            <cfset setProperty('myVar2',aStruct)>
            <cfset setProperty('myVar3',aQuery)>
            <cfset setProperty('myVar4',anObject)>
            <cfset setProperty('myVar5',dataFromAService)>
        </cffunction>

    </cfcomponent>

Use the variables in your app.

The variables you have set in step 2 can then be used throughout your app by using the getProperty() method

    <cfset aLocalVar=getProperty('myVar1')>

They can also be accessed in Coldspring as below:

    <bean id="someBean" class="dot.path.to.CFC">
        <property name="serverName"><value>${myVar2}</value></property>
    </bean>

Back to FAQs

Clone this wiki locally