[Coldbox 4] Suggestions for onAppStart settings

We are converting a legacy application to a CB4.0 application, and could use some assistance in figuring out how to proceed. I have an object that I want to instantiate during application startup and then be configured (not during init, but a separate function), … then will be available throughout the application lifecycle… in handlers, views, layouts. What would the best way to handle this be? Thanks!

There are a few ways to do this:

  1. Include your object instantiation in your onApplicationStart() method of Application.cfc

  2. Use Wirebox and specify your config method and parameters, setting the object as a singleton. It will then persist in the Application scope and you can inject the instance, as needed.

  3. Use a Request Context Decorator and use your configure() method to check for the existence of the object instance in Application scope, handling any instantiation and configuration as needed.

IMHO, the Wirebox method would be the preferred method for handling this and would give you more flexibility as you migrate the legacy app fully in to Coldbox.


Jon’s answer is great. I think this simply sounds like a singleton. Just create the CFC and add the “singleton” annotation to it"

component singleton {}

And then just inject it wherever you need it. Let WireBox handle its creation and persistence. Unless there is something particularly unique about how this component is configured I don’t see why reason to do anything special. Can you explain the requirement for when you want to configure it, and why it can be in the init(), or at least onDIComplete()?