org.apache.commons.inject.api
Interface IInjector

All Known Implementing Classes:
AbstractInjector, DefaultInjector

public interface IInjector

The injector is used to build objects for your application. It does so by maintaining a map of keys and associated bindings. The map is created by modules. Some bindings are present automatically: - The Injector itself. - For any IBinding (binding of type T), there is also a provider of type T. To create an injector, use the class CommonsInject.


Method Summary
<T> T
getInstance(Class<T> pType)
          Returns an instance of pType, if a matching binding is present.
<T> T
getInstance(Class<T> pType, String pName)
          Returns an instance of the given type, with the given name, if a matching binding is present.
<T> T
getInstance(IKey<T> pKey)
          Returns an instance of the binding that has been registered for the given key.
 void injectMembers(Object pInstance)
          Injects members into the given instance, as if it where created by the injector itself.
<T> T
requireInstance(Class<T> pType)
          Returns an instance of pType, if a matching binding is present.
<T> T
requireInstance(Class<T> pType, String pName)
          Returns an instance of the given type, with the given name, if a matching binding is present.
<T> T
requireInstance(IKey<T> pKey)
          Returns an instance of the binding that has been registered for the given key.
 

Method Detail

getInstance

<T> T getInstance(Class<T> pType)
Returns an instance of pType, if a matching binding is present. This is a shortcut for
   getInstance(pType, "")
 
or
   getInstance(pType, Key.NO_NAME)
 

Parameters:
pType - The requested type.
Returns:
The created instance, or null.
See Also:
getInstance(Class, String), getInstance(IKey), requireInstance(Class)

getInstance

<T> T getInstance(Class<T> pType,
                  String pName)
Returns an instance of the given type, with the given name, if a matching binding is present. This is a shortcut for
   Key key = new Key(pType, pName);
   getInstance(key)
 
or
   Key key = new Key(pType, pName, Key.NO_ANNOTATIONS);
   getInstance(key)
 

Parameters:
pType - The requested type.
pName - The requested objects name.
Returns:
The created instance, or null.
See Also:
getInstance(IKey), requireInstance(Class, String)

getInstance

<T> T getInstance(IKey<T> pKey)
Returns an instance of the binding that has been registered for the given key.

Parameters:
pKey - A binding key, for which a binding has been registered.
Returns:
The created instance, or null.
See Also:
getInstance(Class), getInstance(Class, String), requireInstance(IKey)

requireInstance

<T> T requireInstance(Class<T> pType)
                  throws NoSuchBindingException
Returns an instance of pType, if a matching binding is present. This is a shortcut for
   requireInstance(pType, "")
 
or
   requireInstance(pType, Key.NO_NAME)
 

Parameters:
pType - The requested type.
Returns:
The created instance.
Throws:
NoSuchBindingException - No matching binding has been registered with the injector.
See Also:
getInstance(Class, String), getInstance(IKey), requireInstance(Class)

requireInstance

<T> T requireInstance(Class<T> pType,
                      String pName)
                  throws NoSuchBindingException
Returns an instance of the given type, with the given name, if a matching binding is present. This is a shortcut for
   Key key = new Key(pType, pName);
   requireInstance(key)
 
or
   Key key = new Key(pType, pName, Key.NO_ANNOTATIONS);
  requireInstance(key)
 

Parameters:
pType - The requested type.
pName - The requested objects name.
Returns:
The created instance.
Throws:
NoSuchBindingException - No matching binding has been registered with the injector.
See Also:
getInstance(Class, String), requireInstance(Class), requireInstance(IKey)

requireInstance

<T> T requireInstance(IKey<T> pKey)
                  throws NoSuchBindingException
Returns an instance of the binding that has been registered for the given key.

Parameters:
pKey - A binding key, for which a binding has been registered.
Returns:
The created instance.
Throws:
NoSuchBindingException - No matching binding has been registered with the injector.
See Also:
getInstance(IKey), requireInstance(Class), requireInstance(Class, String)

injectMembers

void injectMembers(Object pInstance)
Injects members into the given instance, as if it where created by the injector itself. In other words, fills fields and invokes methods annotated with @Inject, assuming that a binding is present for those fields, and method parameters.



Copyright © 2014 The Apache Software Foundation. All Rights Reserved.