More questions on injection (builtin with 2.6.2)

I'm trying to follow along with the example that came with 2.6.2 beta
release here:
http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbWhatsNew2.62#SimpleExample

The example doesn't seem to work the way its written or perhaps I'm
missing something. We use AddModelMapping to define an alias for
references to our model objects. That makes sense. But then getModel
isn't actually used in the example anywhere. Does the declaration of a
cfproperty with type="model" do the magic autowiring? Like here in the
UserService.cfc:
<cfproperty name="UserGateway" type="model" scope="instance" />

And then in the handler, the cfcomponent has an attribute of
autowire="true" which I presume tells Coldbox to go look for
dependencies. And there is a a cfproperty that tells it to go grab the
UserService:
<cfproperty name="UserService" type="Model" scope="instance" />

Presumably the type="model" tells it to refer to our ModelMappings and
find the one named UserService and get the value, which is
security.UserService

But when I try to implement the example code, it chokes when it gets
to instantiating the UserService:
rc.qUsers = instance.UserService.getAllUsers();

"Error Messages: Element USERSERVICE is undefined in a Java object of
type class [Ljava.lang.String;."

So it doesn't seem to be autowiring the dependency as it ought to. I
feel like I'm getting close to understanding and being able to use DI
but its not quite there.

Any thoughts?

Thanks in advance,
Judah

Does the declaration of a
cfproperty with type=“model” do the magic autowiring? Like here in the
UserService.cfc:

Exactly!! You can use cfproperty to inject model objects around. GetModel() is only available for handlers, plugins and interceptors, if you would like to get a specfic model object.

So it doesn’t seem to be autowiring the dependency as it ought to. I
feel like I’m getting close to understanding and being able to use DI
but its not quite there.

One final check here Judah, make sure the autowiring interceptor is declared in your configuration file. By default in the new versions, the applicationtemplate has this defined.

Make sure that is declared in your coldbox.xml. This is what tells the framework that autowiring is supported for the application.

Please confirm

Does the declaration of a

cfproperty with type="model" do the magic autowiring? Like here in the
UserService.cfc:
<cfproperty name="UserGateway" type="model" scope="instance" />

Exactly!! You can use cfproperty to inject model objects around. GetModel()
is only available for handlers, plugins and interceptors, if you would like
to get a specfic model object.

The concepts seem to be making it into my skull, it appears to be the
execution that is faulty. :slight_smile:

So it doesn't seem to be autowiring the dependency as it ought to. I

feel like I'm getting close to understanding and being able to use DI
but its not quite there.

One final check here Judah, make sure the autowiring interceptor is declared
in your configuration file. By default in the new versions, the
applicationtemplate has this defined.

<Interceptor class="coldbox.system.interceptors.autowire" />

Make sure that is declared in your coldbox.xml. This is what tells the
framework that autowiring is supported for the application.

Please confirm

yes, that interceptor is in there and I haven't modified that section
from the install.

<!-- USE AUTOWIRING -->
<Interceptor class="coldbox.system.interceptors.autowire">
  <Property name='enableSetterInjection'>true</Property>
</Interceptor>

This may seem like a stupid question, but have you done a fwreinit since adding the tags, etc?

Not a stupid question at all, but yes, I did. Although I don't think
that a cfproperty would need a fwreinit would it? Coldbox picks up new
methods and properties as they are created, I thought, and I'd just
need to reinit if I changed something in the coldbox config file.

Judah

If you are using handlerCaching, then you will need to reinit, because, well, they are cached.

For development, make sure handlercaching is set to false and HandlersIndexAutoReload is set to true. Those are the defaults of the app template for development.

Anyways, I don’t see where this is failing, except that in the docs I forgot to extend the handler: extends=coldbox.system.eventhandler. Please make sure that it does.

I have the same code from the docs working. Please post more…