[coldbox 3.6.0] Question on using an injected object in another objects Init.

I have wirebox enabled and use it for injecting objects.

I am running into a situation where I am getting errors and wanted to know if there was some way to make it work.

I have two objects lets call them OBJ_A and OBJ_B. and OBJ_A is injecting OBJ_B
OBJ_A then needs to use some method of OBJ_B for it’s initialization.

OBJ_A is defined like this , assume that OBJ_B is written correctly with a function that returns someImportantNumber;

`
component OBJ_A
output=“false”
accessors=true
{
property OBJ_B inject=“OBJ_B”;
property int someImportantNumber;
function init () {
setSomeImportantNumber(OBJ_B.getSomeImportantNumber());
return this;
}
}.

`

This is giving me an error saying that OBJ_B does not have a valid constructor function. I’m assuming that this is because at this point they way it is coded OBJ_B has not been created yet in OBJ_A.

If I change the code to run to use another function that I call separately then it works fine.

`
component OBJ_A
output=“false”
accessors=true
{
property OBJ_B inject=“OBJ_B”;
property int someImportantNumber;
function init () {
return this;
}

function setImportNumber(){
setSomeImportantNumber(OBJ_B.getSomeImportantNumber());
}
}.

`

So my question comes down to is this the way it is suppose to work or do I have some configuration problem?

And or is there a different way to reference OBJ_B so that it is available to OBJ_A’s init function?

Keo,

By putting the inject annotation on the property, Wirebox is only injecting OBJ_B after OBJ_A is intantiated. So when the init is called, there’s nothing being passed into the constructor so OBJ_B is not there yet.

What you want to do is inject into the constructor so it’s available at init time, here’s how:

component OBJ_A ouput=“false” accessors=“true” autowire=“true”
{
property name=“OBJ_B”;
property name=“someImportantNumber” type=“Numeric”;

/**

  • constructor method
  • @output false
  • @objB.inject OBJ_B
    */
    public OBJ_A function init(required any objB)
    {
    // if you need to retain obj b within obj_a for use post init then set it into the property else obj b property defined above is not needed and you simply get the number value you are after into the some important number property.
    setOBJ_B(objB);
    setSomeImportantNumber(objB.getSomeImportantNumber());
    }

}

Notice that now OBJ_B is being injected into the constructor method via the annotation “@objB.inject OBJ_B” and will now be available at init runtime.

Hope this helps,

Best of luck

Phill Nacelli
Sr. Software Architect, Solutions Lead
AboutWeb, LLC

Thats perfect, with all of the different ways to do injections I figured that one of them had to work.

I will try this out, Thanks for your quick reply.

You probably need to use OnDIComplete. The injected properties are not available at time of INIT() so you tell the INIT() function and wirebox that when the initialization of the the class is done, follow up with the OnDIComplete() function. I have to do this from time to time too.

function init () onDIComplete {
return this;
}

function OnDIComplete() {
setSomeImportantNumber(OBJ_B.getSomeImportantNumber());
}

Mike

Thanks I will give that a try as well.