Time to code! woohoo! I am going to carry on with the components from the last post which are userService and userDAO in a "model" directory on the root. For this post we'll assume that my DAO only has basic CRUD methods; Create, Read, Update, and Delete
I am going to demonstrate the Constructor Injection method of dependency injection which was summarised quickly in my last post. I think a recap is need here:
This tag will cause Coldspring to supply your bean with a value or object reference when it is instantiated (during a CFC's init() method), passed as an argument named via the name attribute.
What the above statement means in basic terms is that the userService must have an init() method which accepts the userDAO object as an argument and sets it to a variable. e.g.
n.b. For the argument type you can put the path to your DAO. In my example it would look like type="model.userDAO". I am not going to though just so I can keep things simple here.
At the moment don't worry how the init() method gets the userDAO object. Just know that it does (via ColdSpring) and when it does it sets it to a local variable. So with the userDAO initiated and assigned to access it and call a method(function) is as easy as
Putting this all together your userService should look like this
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
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 <constructor-arg> tag to the bean and reference the userDAO
And there you have it, that's it. I'm not going to go into much more details on this one as I only wanted to explain the basics to get people up and running.
Tha....tha..tha...that's all folks for this part. Next part (next week now) will be on setter injection
Don't forget for more information check outcoldspringframework.org, or the mailing lists
To subscribe, send an email:
Subject: SUBSCRIBE email@example.com