com.google.inject
Interface Container


public interface Container

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

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 create and inject an instance of Foo:

  Container c = ...;
  Foo foo = c.inject(Foo.class);
 

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

Method Summary
<T> T
getInstance(java.lang.Class<T> type)
          Convenience method. Equivalent to get(type, DEFAULT_NAME).
<T> T
getInstance(java.lang.Class<T> type, java.lang.String name)
          Gets an instance of the given dependency which was declared in ContainerBuilder.
 boolean hasBindingFor(Key<?> key)
          Checks whether the container has a binding for given key.
<T> T
inject(java.lang.Class<T> implementation)
          Creates and injects a new instance of type implementation.
 void inject(java.lang.Object o)
          Injects dependencies into the fields and methods of an existing object.
 void removeScopeStrategy()
          Removes the scope strategy for the current thread.
 void setScopeStrategy(Scope.Strategy scopeStrategy)
          Sets the scope strategy for the current thread.
 

Method Detail

inject

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


inject

<T> T inject(java.lang.Class<T> implementation)
Creates and injects a new instance of type implementation.


getInstance

<T> T getInstance(java.lang.Class<T> type,
                  java.lang.String name)
Gets an instance of the given dependency which was declared in ContainerBuilder.


getInstance

<T> T getInstance(java.lang.Class<T> type)
Convenience method. Equivalent to get(type, DEFAULT_NAME).


setScopeStrategy

void setScopeStrategy(Scope.Strategy scopeStrategy)
Sets the scope strategy for the current thread.


removeScopeStrategy

void removeScopeStrategy()
Removes the scope strategy for the current thread.


hasBindingFor

boolean hasBindingFor(Key<?> key)
Checks whether the container has a binding for given key.

Parameters:
key - binding key
Returns:
true if a binding exists for the given key