Does cfproperty injection need setter methods too?

Actually trying to build something w ColdBox for the first time,
didn't get cfproperty-based DI working with M6. I know about
'inject="model"', have a model mapping in place. No errors reported
(at least until my code dies when the injected item isn't defined).

Do I need a setter for each injectable property? I thought not, but
thought I should check with more experienced folks.


Nope, not needed.


You do not need to inject Bean files at this stage.

ColdBox won't inject the model if it can't resolve the mapping.

Make sure you include the model mapping alias in your injection


You might also want to make sure Autowire is a registered interceptor
in your config file.

Verify your models are defined in your config/modelmappings.cfm file.


The above line will allow ColdBox to find your model with the alias ‘myModel’.

You could also provide the alias of your choosing:


Within your model, make sure you have autowire=true within your CFC declaration.

And, make sure your cfproperty looks like this:

property name=‘myModel’ inject=‘model’;

Or, you could do this to name your injected model something else within your model receiving the injection. You just need to make sure you reference the right alias.

property name=‘SomeOtherName’ inject=‘model:myModel’;

Hope that helps.

Hope that helps.

Thanks Aaron. Project is home and I'm at work so I can't verify right
now, but reading a section of the docs I'd somehow not seen before, I
think you're right, I missed autowire=true.


Dave you can also enable logging for the beanfactory plugin via logbox like

Debug = ["coldbox.system.plugins.BeanFactory"]

This enables the logging of the dependency injection resolutions so you can see what's wrong

Still stuck; details below, but first, thanks for jumping in Luis.
Sorry to be thick/new, but where does this code/config go?

Dave you can also enable logging for the beanfactory plugin via logbox like

Debug = ["coldbox.system.plugins.BeanFactory"]

This enables the logging of the dependency injection resolutions so you can see what's wrong

Here's what I think are the relevant bits. What's missing or wrong?
3.0 M6 download.





<Interceptor class="coldbox.system.interceptors.Autowire">
  <Property name="debugMode">true</Property>


addModelMapping('CalendarService', '');

<cfcomponent extends="coldbox.system.EventHandler" output="false"

<cfproperty name="CalendarService" inject="CalendarService">
<!--- also tried inject="" --->

  this gets hit with cfproperty enabled, not when it's commented out
  CalendarService isn't in that StructKeyList
<cffunction name="onDIComplete" output="true" returntype="void">
  <cfhtmlhead text="#StructKeyList(variables)#">

<!--- Default Action --->
<cffunction name="index" returntype="void" output="false" hint="My main event">
  <cfargument name="event" required="true">
    var rc = event.getCollection();
    rc.welcomeMessage = "Welcome to Stephentown!";
    // next line crashes, CalendarService not defined
    rc.CalendarService = CalendarService;

Thanks for any ideas, I'll keep trying things too.


Ok, some answers to my own questions, with the help of a friend w more
CB experience, for the benefit of anyone looking at this later:

First, my model apping was wrong, needs to start from the root of the model dir:
   addModelMapping('CalendarService', 'services.CalendarService');

Second, the cfproperty tag should look like this:
   <cfproperty name="CalendarService" inject="model">

That looks for the mapped model whose name matches the injected
property name. If your local name doesn't match the mapped name, you
can do this:
   <cfproperty name="LocalCalendarService" inject="model:CalendarService">

Some remaining questions:

- Why didn't something complain when I tried to inject a model that
doesn't exist? Can I configure ColdBox so that throws an error?
Nothing's going to work until I get my mappings sorted (not that it
should be hard now that I've got some basics together), so it should
blow up if they're wrong.

- Off this main topic, am I right that the simplest way to dump
something to the ColdBox debug panel is like this:
   logger = logbox.getLogger('default');''CalendarService', CalendarService);

Thanks for your help and patience with my CB-newbie questions,


Well, I’m not one of the ColdBox team, but I’d say the because the cfproperty tags aren’t specific to ColdBox, ColdBox just assumes that you are using the cfproperty metadata for some other purpose (other that ColdBox DI) and doesn’t throw an error.

The logger is a great way of logging info, but a quick and dirty way to see info in the debugging panel (from a handler) is to stick it in the rc scope, something like:

<cffunction name=“index” access=“public” returntype=“void” output=“false”>

<cfargument name=“Event” type=“any”>
<cfset var rc = Event.getCollection()>

<cfset rc.CalendarService = variables.CalendarService>
<cfset Event.setView(“home”)>

I see your point, but it seems like inject= is ColdBox-specific. If
you're using it, you mean to inject something via ColdBox's
infrastructure, and it should complain if that fails.

As to examining injectables by sticking them in rc, did that, but it
blows up if it's not defined, like references in real code do. What I
was trying to figure out is *why* it wasn't defined when I thought I'd
injected it.


That’s probably a historic thing as ColdBox used to use the type attribute for the DSL. ColdBox does have a debug mode for the AutoWiring which you can enable in your config.

<Interceptor class="coldbox.system.interceptors.autowire">
  <Property name="debugMode">true</Property>

Hope that helps.

  • John

Thanks so much enigment! It was the "needs to start from the root of
the model dir" line that fixed all of my problems! I didn't notice
that at all in the documentation, so thank you for bringing it up

Glad some of my fumbling around helped you out :slight_smile: