Aliaspooryorik
ColdFusion ORM Book

ColdSpring DI with onMissingMethod

I've had the chance to play around with ColdSpring today. One of the things I wanted to know was if you could use it with onMissingMethod to pass in dependancies.

In ColdSpring you can either choose to have all the dependency injection handled for you auto-magically (using the default-autowire="byName"' attribute of the beans node) or define them yourself, or even a combination of the two!

Here's a quick snippet of our config when using autowire if MyObject has a dependency on SomeObject:


<bean id="SomeObject" class="mypath.SomeObject" />
<bean id="MyObject" class="mypath.MyObject" />

ColdString figures out that there is a method called setSomeObject in MyObject and automatically passes SomeObject to it. Clever stuff.

Here's a quick snippet of explicitly passing the dependency without using autowire:


<bean id="SomeObject" class="mypath.SomeObject" />
<bean id="MyObject" class="mypath.MyObject">
  <!-- set the dependancy -->
  <property name="SomeObject">
  <ref bean="SomeObject" />
  </property>
</bean>

The autowire will only work if you have a method called setSomeObject. However when you explicitly pass the dependency you can use onMissingMethod without having to actually create a setSomeObject method.

Personally I prefer to have a concrete API and tend not to use onMissingMethod, but it is interesting to know that you can do it. It could be quite useful for rapid prototyping (although you loose the ability to use autowire).

 


2 comments

  1. I generally don't like autowire - it's too easy to have a setter called "accidentally" by ColdSpring and, in its own way, the XML definition of dependencies is an "API" between the objects.

    However, it's very interesting to know that explicit dependencies allow the use of onMissingMethod() - I hadn't tried that and I'm a big fan of oMM :)

    Comment by Sean Corfield – March 06, 2009
  2. Hi Sean, like you I don't tend to use autowire either because I like to see the dependencies, there is the performance hit to consider (on application start) which can be a problem. I hadn't thought about accidental injection but it's a good point.

    I seem to remember someone doing a presentation on onMissingMethod at SOTR 08... :)

    Comment by John Whish – March 06, 2009

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.

Please subscribe me to any further comments
 

Search

Wish List

Found something helpful & want to say ’thanks‘? Then visit my Amazon Wish List :)

Categories

Recent Posts