Looking for better understanding of Constructor & Setter Dependencies


I am new to certain things, such as ColdSpring and Transfer, so pardon
the question. I was reading up on injection (nothing like having old
code using 'type' and mixing with 3.x, hehe) and came across the
Constructor & Setter Dependencies usage. I've used autowire and and
using the new inject syntax so get that, just don't understand the

I tried and failed to implement, so I know I am missing something. Is
there a more fully baked example somewhere or article(s) I should
start with.

Thanks for the time.


I’m not sure I am properly understanding the question, but I’ll try to answer anyways.

Constructor injection is where you have an argument on your constructor method (typically init) that takes the dependency as its value. For example…

In the above example, whenever init() is called, the UserService is required to be passed in.

Setter injection, on the other hand, means that the injection is made when a matching setter is found during introspection. So, something like the following…

So, if ColdSpring is handling DI for you and it has a UserService bean defined, then it would automatically call setUserService() for you and inject the dependency.

Does that make sense?

Matt that I get. What I am referring to is the example in the online Wiki docs ( http://wiki.coldbox.org/wiki/Models.cfm ) which shows the following

<— Constructor Markers —>

<— Setter Markers —>

In this the ‘dsn’ is autowire and is ‘orm’. So to me that says anything within init() can use ‘arguments.dsn’ to get the information. What I am wondering is how would other functions with the model file use that dsn after the the model.init() has been called?

Can this take the place of a cfproperty injection? if so what is the benefit.

Hope that makes better sense. Thanks for the education


Hi Kevin,

The annotations you see on the constructor is to tell the autowire
injector what to inject when constructing the target object. Once the
constructor is called, it would be YOUR responsibility to persist them
in your local components scopes.

function init(dsn, orm){
   variables.dsn = arguments.dsn;
   variables.orm = arguments.orm;

If you use property injection, then the injector will take care of
putting the dependency on the variables scope for you. Does thi make
sense now?

Ahhhh, ok. Yes the mud is clearer. So the next question would be why you would use the Constructor method versus the cfproperty method and what are the advantages.

Sorry I know this is kinda bassackwards approach to learning some OO concepts, but that’s how I roll sometimes :wink:


I don’t think property injections are available at the time the object is constructed. So the constructor method is available to handle the case when you need injections at the time of construction. Any other time, cfproperty can be used.

  • Gabriel

Yeah that makes sense and now that I think of it, is in the documentation. Guess for me I would need a use case where that would be necessary. Just want to make sure I use it when appropriate.


There are huge papers written on the pros/cons of constructor injection vs setter/property injection. Here is my short take on it.

Constructor injection is definitely best suited on strong typed languages and also on languages that offer constructors and final properties. This allows for the injection to occur at construction time and the depenencies set as immutable. Most commonly know as “good citizen” objects. Once created you are guaranteed of their dependencies and startup or constructor code that relies on these dependencies will exist. The cons, are circular dependencies and immutability, which in CF makes no sense.

Setter/Property injection occurs after construction, so circular depenencies do not affect it as constructor does. However, you cannot use your dependencies until your setters are called or properties injected. So state of objects is unknown until injection is finished. Setter injection requires the creation of methods to enable incoming dependnecies to be saved. To me this is unecessary and creates a bunch of cruft if you have enough dependencies. Property approach is cleaner as it is metadata that is placed on the component at hand. No need for setters created and it is highly documentable.

Again, some debates between constructor and later dependency injection in languages where you can have immutability does not translate to CF, plus constructors in CF are not implicitly called (until 9), plus you can call cf constructors more than once which in other languages (like java) you can’t.

So in conclusion, I usually prefer property injection over constructor injection, unless there is something that definitely needs to be done at construction time.

Hope this helps.

Luis F. Majano
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

It does, Thanks for taking the time.