AndyJarrett

ColdSpring and Dependency Injection for the beginner part 3- Setter Injection.

Onto part 3 of this rough beginners tutorial. By now you should of read part 1 and part 2 and this is the final part of me explaining Dependency Injection. This doesn't mean that come the end of this post you'll now know everything. But you'll know the basics, and be able to walk away and understand a little more of how and where to use ColdSpring.

To go back a little as since my first post there has been for my American readers a national holiday - Deep Fried Turkey day or Thanksgiving as it is better known (sorry I had to mention the deep fried [whole]turkey, I don't know if it because I'm British, but it just seems wrong!). Back to the code.....todays topic is is Setter Injection and to recap from the ColdSpring site;

<property name="propertyName" />
Similar in nature to constructor arg, however in this case ColdSpring will pass some value or object reference into your bean as an argument to a setter method, identified via the name attribute. Thus, your CFC must have a setter method name that matches the property tag's name attribute (for example if your property is named "foo" then your CFC needs a setFoo() method).

What does this mean. Basically that whatever CFC (for us its the userService) is going to have another component injected (our component to be injected is the userDAO) it will need to have a setter method. Lets us use a blank userService and see what this means:

Again, don't worry about how the method will be receiving the userDAO component. Though after reading part 2 you should be able to guess how! Lets carry on. With this method in the userService its the equivalent to the init() method in the Constructor Injection method. If we continue we can look at a calling function in the userService.cfc that will call the userDAO.cfc:

Putting these two code snippets in to the complete cfc you should have the following:

n.b. I don't know why but setters I always put at the bottom of my components and init() methods at the top. I'm not saying do as I do but just try and be consistent as to where you put these methods.

Lets move onto the ColdSpring.xml file and here we are going to copy what I wrote in part 2. Actually I'm cutting and pasting (it's cheating I know, but we are all friends here) the first part here as it is exactly the same

Now comes the part I told you not to worry about. This is the injection. This is how we tell Coldspring to do this. First off we need to let CS know about userDAO which is one line of xml

Its nothing fancy. In a nutshell it just saying to Coldspring "Do me a favour and register model.userDAO and assign it a friendly name of userDAO for reference later". That was simple.

Now we do the same for the userService

As you can tell the above is the same as from my previous post and there's no point re-wording what I've already typed. Moving on to the setter injection methods and here is when the xml changes slightly. At this point we have two beans registered with ColdSpring, but our userService has no knowledge of the userDAO and vice versa. To make the userService aware is still simple, we just add the <property> tag to the bean and reference the userDAO.

The big difference is the use of the <property> tag. This tags uses the set() to perform it's method of injection. If you are going to inject more objects into the userService you just need to add the properties to the ColdSpring.xml and add the setters to the userService.cfc. This is really it.

It feels like a bad time to end this tutorial but thats it, well all that I wanted to write about for now. Not in a bad way but the idea in my head was to help at least one person to understand how and where to use ColdSpring. Im not going to go into which method is better (you can start at the CS mail archives) and the only reason I won't say which is better is because I haven't got a case to prove a point one way or the other.

As I close this I also want to say this is just the surface. There is a ton of resources out there in blogs. Start of with the search functions on most aggregators:

Plus, don't forget for more information check out

coldspringframework.org, or the mailing lists

To subscribe, send an email:

To: MDaemon@coldspringframework.org

Subject: SUBSCRIBE coldspring-dev@coldspringframework.org