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>
getCreator(java.lang.Class<T> implementation)
          Gets a factory which injects the given class's constructor and creates new instances of T.
<T> Factory<T>
getFactory(Key<T> key)
          Gets the factory bound to the given key.
 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.


getCreator

<T> Factory<T> getCreator(java.lang.Class<T> implementation)
Gets a factory which injects the given class's constructor and creates new instances of T.


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.