In case you were wondering if Mach-II could get any cooler, the answer is *yes*!
For a few versions now, Mach-II has added the ability to inject ColdSpring beans in to your framework components (listeners, plugins, and filters) by use the parameter resolveMachIIDependencies when instantiating the ColdSpring plugin or property.
Take this example… Say that I have a LoginListener.cfc that is dependent on a ColdSpring bean LoginService that lives in our /com directory. I would first define that bean in our ColdSpring config like this:
<bean id="LoginService" />
Then, in our LoginListener we would need to create a setter that matched the Bean that we have defined in ColdSpring like so:
<cffunction name="setLoginService" access="public" output="false" returntype="void"> <cfargument name="LoginService" required="true" type="com.LoginService" /> <cfset variables.LoginService = arguments.LoginService /> </cffunction>
By taking these two actions, and insuring that your LoginService as an init() method as a constructor, when you initialize your application, variables.LoginService will automatically be available to you as an instance of your LoginService bean. That alone was pretty dang cool.
But wait there’s more!
With Mach-II 1.6, this process has become even less cumbersome. In some CFML tags, you can add unexpected attributes without throwing exceptions and are in essence ignored to offer a better solution for managing dependent objects. By default, Mach-II will look for a “depends” attribute in your tags, which can contain a comma separated list of your dependencies. For our simple LoginListener, our tag would look like this:
<cfcomponent output="false" extends="MachII.framework.Listener" depends="LoginService">
Now, instead of repetitive getters/setters, just by merely having that attribute, our LoginListener will now contain the method getLoginService() which will return an instance of the LoginService. That is just ridiculously easy!
As I said, the attribute actually accepts a comma separated list, so as we add dependencies, our tag might look more like this:
<cfcomponent output="false" extends="MachII.framework.Listener" depends="LoginService,UserService,LoggingService">
If you have made it this far, you have probably deducted that we will now have access to getLoginService(), getUserService(), and getLoggingService().
As I mentioned earlier, Mach-II will look for the attribute “depends” by default, but you can customize it to use other attribute names if you wish. You can find that information and more on the Mach-II wiki.