| Package | potomac.inject |
| Class | public class Injector |
| Inheritance | Injector Object |
Developers may use [Inject] to trigger injection on classes created through the Injector. The inject tag may include attribute names that match the argument or variable names to associate the arguments with named bindings. For example:
[Inject(field1="green")]
public var field1:IInterface;
[Inject(arg1="red",arg3="blue")]
public function injectHere(arg1:IInterface,arg2:IInterface,arg3:IInterface):void
{
...
Injection bindings can be created declaratively through [Injectable] or programmatically through bind(). The injectable tag supports parameters to build out injection rules. The attributes are:
| boundTo | Fully qualified interface or class name that becomes the binding's front class. |
| implementedBy | Fully qualified class name of the implementation class. If not specified, the class where the [Injectable] was declared is assumed. |
| named | A unique string which differentiates this binding from others bound to the same type. |
| providedBy | A fully qualified class that implements IProvider. Providers allow programmatic control of injection class creation. |
| singleton | True/false. If true the injector will only create one implementation instance for this binding. |
| asyncInit | True/false. If true the injector allow asynchronous initialization code before declaring the object ready. When true, injectables must extend flash.events.EventDispatcher and dispatch an InjectInitEvent when complete. Use sparingly as this feature is incompatible with some synchronous injection features. |
Examples:
[Injectable(boundTo="package.IInterface",named="green",providedBy="package.MyProvider",singleton="true")]
public class InterfaceImpl {
...
The primary Potomac injector is available for injection. It is bound directly to Injector.
| Method | Defined By | ||
|---|---|---|---|
Injector(bundleService:IBundleService)
Creates a new injector. | Injector | ||
Starts the creation of a injection binding to the given class. | Injector | ||
getInstance(className:String, named:String = null, listener:Function = null):InjectionRequest
Creates an instance (asynchronously) of the given injection rule. | Injector | ||
[static]
Returns the global Injector instance. | Injector | ||
getInstanceImmediate(clazz:Class, named:String = null):Object
Creates an instance (synchronously) of the given injection binding. | Injector | ||
Creates an instance (asynchronously) of the class where the given extension was declared. | Injector | ||
injectInto(object:Object):void
Injects dependencies into the given object synchronously. | Injector | ||
injectIntoAsync(object:Object, listener:Function = null):InjectionRequest
Injects resources into the specified object asynchronously. | Injector | ||
| Injector | () | Constructor |
public function Injector(bundleService:IBundleService)Creates a new injector.
ParametersbundleService:IBundleService — The main Potomac bundle service which the Injector will use to load bundles as necessary.
|
| bind | () | method |
public function bind(clazz:Class):INamerStarts the creation of a injection binding to the given class. Use the returned INamer to continue to injection binding. Continue to use the returned object from subsequent calls to build the binding.
Parameters
clazz:Class — Class instance of a class or interface to create a binding for.
|
INamer — an INamer allowing further creation of the binding.
|
myInjector.bind(IInterface).named("myName").toClass(IImplementation).asSingleton();
| getInstance | () | method |
public function getInstance(className:String, named:String = null, listener:Function = null):InjectionRequestCreates an instance (asynchronously) of the given injection rule. If a listener function is given, the listener will be called automatically when the instance is created, otherwise the caller must add a listener to the returned InjectionRequest.
Parameters
className:String — Class name of the binding.
| |
named:String (default = null) — Name attribute of the binding.
| |
listener:Function (default = null) — Function to call when creation and injection are complete.
|
InjectionRequest — An injection request. If the caller supplied a listener function, the return value can be safely
ignored.
|
| getInstance | () | method |
public static function getInstance():InjectorReturns the global Injector instance.
This method goes against the design of dependency injection itself but is made available for pragmatic reasons. It is unexpected that developers would mock the Injector itself and so this method is not considered harmful.
ReturnsInjector |
| getInstanceImmediate | () | method |
public function getInstanceImmediate(clazz:Class, named:String = null):ObjectCreates an instance (synchronously) of the given injection binding. This method should be used with caution as it assumes all necessary bundles are loaded to satisfy the injection requests and that all required [Injectable]s necessary to satisfy the instance's creation do not require asynchronous initialization.
Parameters
clazz:Class — Class of the injection binding.
| |
named:String (default = null) — Name attribute of the injection binding.
|
Object — An instance of the injection binding.
|
| getInstanceOfExtension | () | method |
public function getInstanceOfExtension(extension:Extension, listener:Function = null):InjectionRequestCreates an instance (asynchronously) of the class where the given extension was declared. If a listener function is given, the listener will be called automatically when the instance is created, otherwise the caller must add a listener to the returned InjectionRequest.
Parameters
extension:Extension — Extension whose declaring class should be created.
| |
listener:Function (default = null) — Listener fired when the instance is created.
|
InjectionRequest — An injection request. If the caller supplied a listener function, the return value can be safely
ignored.
|
| injectInto | () | method |
public function injectInto(object:Object):voidInjects dependencies into the given object synchronously. Caution should be used when calling this method as it assumes all [Injectable]s required to satisfy the injections do not require asynchronous initialization.
Parameters
object:Object |
| injectIntoAsync | () | method |
public function injectIntoAsync(object:Object, listener:Function = null):InjectionRequestInjects resources into the specified object asynchronously. Using this asynchronous method is necessary if one or more [Injectable]s that need to be created require asynchronous initialization.
Parameters
object:Object — Object to inject into
| |
listener:Function (default = null) — Listener that is dispatched an InjectionEvent.INJECTINTO_COMPLETE event when injection is complete.
|
InjectionRequest — An InjectionRequest token.
|