com.google.inject
Interface Container


public interface Container

Injects dependencies into constructors, methods and fields annotated with @Inject.

When injecting a method or constructor, you can additionally annotate its parameters with @Inject and specify a dependency name. When a parameter has no annotation, the container uses the name from the method or constructor's @Inject annotation respectively.

For example:

  class Foo {

    // Inject the int constant named "i".
    @Inject("i") int i;

    // Inject the default implementation of Bar and the String constant
    // named "s".
    @Inject Foo(Bar bar, @Inject("s") String s) {
      ...
    }

    // Inject the default implementation of Baz and the Bob implementation
    // named "foo".
    @Inject void initialize(Baz baz, @Inject("foo") Bob bob) {
      ...
    }

    // Inject the default implementation of Tee.
    @Inject void setTee(Tee tee) {
      ...
    }
  }
 

To get an instance of Foo:

  Container c = ...;
  Key<Foo> fooKey = Key.get(Foo.class);
  Factory<Foo> fooFactory = c.getFactory(fooKey);
  Foo foo = fooFactory.get();
 

Author:
crazybob@google.com (Bob Lee)
See Also:
ContainerBuilder

Method Summary
<T> java.util.List<Binding<T>>
findBindingsByType(TypeLiteral<T> type)
          Finds all bindings to the given type.
<T> Binding<T>
getBinding(Key<T> key)
          Gets a binding for the given key.
 java.util.Map<Key<?>,Binding<?>> getBindings()
          Gets all bindings.
<T> Factory<T>
getFactory(java.lang.Class<T> type)
          Gets the factory bound to the given type.
<T> Factory<T>
getFactory(java.lang.Class<T> type, java.lang.String name)
          Gets the factory bound to the given type and name.
<T> Factory<T>
getFactory(Key<T> key)
          Gets the factory bound to the given key.
<T> Factory<T>
getFactory(TypeLiteral<T> type)
          Gets the factory bound to the given type.
<T> Factory<T>
getFactory(TypeLiteral<T> type, java.lang.String name)
          Gets the factory bound to the given type and name.
<T> T
getInstance(java.lang.Class<T> type)
          Gets an instance from the factory bound to the given type.
<T> T
getInstance(java.lang.Class<T> type, java.lang.String name)
          Gets an instance from the factory bound to the given type and name.
<T> T
getInstance(Key<T> key)
          Gets an instance from the factory bound to the given key.
<T> T
getInstance(TypeLiteral<T> type)
          Gets an instance from the factory bound to the given type.
<T> T
getInstance(TypeLiteral<T> type, java.lang.String name)
          Gets an instance from the factory bound to the given type and name.
 void injectMembers(java.lang.Object o)
          Injects dependencies into the fields and methods of an existing object.
 

Method Detail

injectMembers

void injectMembers(java.lang.Object o)
Injects dependencies into the fields and methods of an existing object.


getFactory

<T> Factory<T> getFactory(Key<T> key)
Gets the factory bound to the given key.


getBindings

java.util.Map<Key<?>,Binding<?>> getBindings()
Gets all bindings.


getBinding

<T> Binding<T> getBinding(Key<T> key)
Gets a binding for the given key.


findBindingsByType

<T> java.util.List<Binding<T>> findBindingsByType(TypeLiteral<T> type)
Finds all bindings to the given type.


getFactory

<T> Factory<T> getFactory(java.lang.Class<T> type)
Gets the factory bound to the given type.


getFactory

<T> Factory<T> getFactory(TypeLiteral<T> type)
Gets the factory bound to the given type.


getInstance

<T> T getInstance(TypeLiteral<T> type)
Gets an instance from the factory bound to the given type.


getInstance

<T> T getInstance(java.lang.Class<T> type)
Gets an instance from the factory bound to the given type.


getInstance

<T> T getInstance(Key<T> key)
Gets an instance from the factory bound to the given key.


getInstance

<T> T getInstance(TypeLiteral<T> type,
                  java.lang.String name)
Gets an instance from the factory bound to the given type and name.


getInstance

<T> T getInstance(java.lang.Class<T> type,
                  java.lang.String name)
Gets an instance from the factory bound to the given type and name.


getFactory

<T> Factory<T> getFactory(java.lang.Class<T> type,
                          java.lang.String name)
Gets the factory bound to the given type and name.


getFactory

<T> Factory<T> getFactory(TypeLiteral<T> type,
                          java.lang.String name)
Gets the factory bound to the given type and name.