In this example, I have a model object called test.cfc
which has a dependency testService.cfc
.
test
has WireBox inject testService
through a property declaration. The object looks like this:
component {
property name="testService" inject="testService";
/**
* Constructor
*/
function init() {
// do something in the test service
testService.doSomething();
return this;
}
}
For reference, testService
has a single method called doSomething()
which dumps out some text:
component
singleton
{
/**
* Constructor
*/
function init() {
return this;
}
/**
* Do Something
*/
function doSomething() {
writeDump( "something" );
}
}
The problem is, WireBox doesn't seem to inject testService
until after the constructor init()
method fires. So, if I run this in my handler:
prc.test = wirebox.getInstance(
name = "test"
);
I get the following error message: Error building: test -> Variable TESTSERVICE is undefined.. DSL: , Path: models.test
Just for sanity sake, if I modify test
so that testService
gets referenced after the object is constructed, everything works fine. The problem seems to be isolated to constructor methods.
How can I make sure my dependencies can be referenced in my object constructor methods? Thanks for your assistance!