Sam Farmer

Growing up I never imagined I would play bass guitar for the Dave Matthews Band. And indeed it never happened.

But I have become passionate about making software.

ColdSpring autowire byType plus ColdFusion 9 accessors means less code

December 7, 2009

During a great presentation by Raymond Camden on ColdSpring (slides) he showed off the cool autowire byType feature which got me thinking about using the property definitions in ColdFusion 9 to write less code. When default-autowire="byType" is set in ColdSpring it will inspect your CFC's and if the type of an argument is a defined bean it will inject it into the CFC.

Lets take a look at how this works:

<cffunction name="setShoppingCartService" access="public" returnType="void" output="false"> <cfargument name="shoppingCartService" type="root.model.ShoppingCartService" required="true"> <cfset variables.shoppingCartService = arguments.shoppingCartService> </cffunction>

The key part is the type attribute of the cfargument tag. If ColdSpring can match a defined bean of that type it will inject that CFC.Very cool but there is a fair amount of code required to do this.

Enter ColdFusion 9. First lets set accessors to true:

component accessors="true" {

With accessors on each defined property will have a getX and setX function. So by writing:

property name="shoppingCartService" type="root.model.ShoppingCartService";

Now, when ColdSpring does autowire coolness it will find the type and inject the CFC.

[UPDATE, 12/7/09 9.12PM EST] For reference here is what the ColdSpring config.xml file looks like:

<beans default-autowire="byType"> <bean id="ShoppingCartService" class="root.model.ShoppingCartService"> <constructor-arg name="dsn"> <value>${dsn}</value> </constructor-arg> </bean> </beans>

Also credit to Raymond Camden for the basis of these examples which where taken from the example code from his presentation. [END UPDATE]

Which is pretty cool stuff and another example of how ColdFusion 9 opens up new possibilities for coding ColdFusion applications.

In the chat session of the presentation Penny brought up the point that autowiring byType, or byName, means that the main ColdSpring config file does not contain information about where a CFC is used. Personally I am fine with that but I think its a valid point and one for each development team and developer to work out.

No Comments

Some comments have been lost over the years due to moving hosts.


More blog entries that I have written.

Code coloring by PRISM.