So I’m working with this legacy codebase, and I’ve got an application.cfc that looks something like this:
<cfcomponent
displayname="Application"
output="true"
hint="Handle the application.">
<!--- Set up the application. --->
<cfscript>
this.name = "myapp";
this.sessionManagement = true;
this.sessionType = "cfml";
this.clientManagement = true;
this.tag.cflocation.addtoken = false;
// ...more stuff
// *Location 1*
// Application Hooks
public boolean function onApplicationStart() {
StructClear(Application);
// *Location 2*
Application.ObjectFactory = createObject("component","com.ObjectFactory").init();
Application.preferenceObjectFactory = application.ObjectFactory.getObject("preferenceObjectFactory");
Application.preferenceObjectFactory.init();
Application.securityObject = application.ObjectFactory.getObject("securityObject");
Application.securityObject.init();
// Bunch more stuff...
}
// ...more hooks
</cfscript>
</cfcomponent>
I wanted to log the version at application startup, and also whenever onApplicationStart()
is called. So at Location 1, I’m logging “Running application version: 1.2.3”, and then at Location 2, I’m logging “onApplicationStart version: 1.2.3”…
I expected the log to show the output from Location 1 once when I restart lucee, and then the output from Location 2 before the first request and any other time onApplicationStart()
is called (it caches a bunch of global prefs from the db in the Application object, so it is called manually when any of those values change)
Instead, Location 2 output is as expected, but I’m seeing Location 1 output before every single request. So it seems that the code in application.cfc is actually rerun on every request, but the onApplicationStart()
hook is actually only run once (when the application is actually started).
So I’m wondering if it’s actually better to not have any executable code in application.cfc outside of the scope of its member functions (Location 1)? The only code that’s currently there is a bunch of member initializations (e.g. this.varname = constantValue;
), so I’m not too worried about the overhead in this case, but I’m wondering about the general conventions/ best practices for how to organize application.cfc.
Like…should that member init code actually be inside onApplicationStart()
or is it ok where it is?
Also, I find the distinction between this.*
(the Application component?) and Application.*
(the Application Scope?) a little confusing. Is there an article somewhere that covers these basic concepts?
Thanks!
-Partap