Autowire Interceptor
The autowire interceptor is a great tool when using object factories like ColdSpring or LightWire. This interceptor will inject dependencies into the following ColdBox objects:
- Plugins
- Handlers
- Interceptors
These dependencies are objects that are created by the object factories. They can be model objects, services, managers, gateways, etc. So we don't have to be constantly using syntax like:
<cfset myService = getPlugin("ioc").getBean("myService")>
As you can see, the typing gets long and well, why not let the framework configure your handler, plugins or interceptor with all the objects it needs. Well now you can!!
1. How to Configure it?
The way to configure the autowire interceptor is via your configuration file. Look below for the sample:
<Interceptor class="coldbox.system.interceptors.autowire"> <Property name="debugMode">true</Property> <Property name="completeDIMethodName">onDIComplete</Property> </Interceptor>
That's it!! So let's go over it. We first declare the autowire interceptor and we set the two available properties discussed below.
| Property | Type | Default | Required | Description |
| debugMode | boolean | false | false | Whether to use the logging facilities on successful/failed injections. |
| completeDIMethodName | string | onDiComplete | false | This takes in the name of a method to fire on the injected object after all dependencies have been injected. This is incredible useful when you need to configure an object right after injections. It uses the onDiComplete convention, but you can use your own. |
Well, that is all to how to declare to use the interceptor.
2. autowire metadata
The next step is to add a new property to the cfcomponent tag of the objects you would like to have autowiring turned on. Simple as that.
autowire={boolean}
<cfcomponent name="handler" output="false" autowire="true">
3. Getters/Setters
Now that you have delcared that the component will be autowired, its time to create those pesky getters and setter methods. Just remember to use the same name as the name of your declared beans in your object factories configuration files. So if you use in your configuration the name userService then create a getter and setter for userService. Again, simple as that.
<cffunction name="getUserService" access="private" returntype="mypath.model.userService" output="false"> <cfreturn instance.userService /> </cffunction> <cffunction name="setuserService" access="public" returntype="void" output="false"> <cfargument name="userService" type="mypath.model.userService" required="true"> <cfset instance.userService = arguments.userService /> </cffunction>
And again, simple as that. So to recap, we declared the autowire interceptor in our configuration file, we added the autowire metadata to our cfcomponent tag and we added our getter and setter methods. Well, you can be done if you want, the last step is optional.
4. Complete DI Method Name
This step is completely optional and only used if you declared it in your config file and you created the appropriate method on your object. What this feature means, is that you can create a method on your object that has the same name as you declared it in your config. The ColdBox default is onDIComplete. So if this method is detected in your object, then ColdBox will execute it AFTER all dependencies have been injected in to the object. This is great for configuring an object once we know all dependencies have been injected.
<cffunction name="onDIComplete" access="private" returntype="void" output="false"> <!--- Call Anything to configure this object ---> <cfset setRoles( getUserService().getAllRoles() )> <cfset validateAllRoles()> </cffunction>
Conclusion
So there you go. Simple as 1-2-3 well and 4 if you are guru status! Very simple and easy to use.
