com.google.inject
Class ContainerBuilder

java.lang.Object
  extended by com.google.inject.spi.SourceConsumer
      extended by com.google.inject.ContainerBuilder

public final class ContainerBuilder
extends SourceConsumer

Builds a dependency injection Container. Binds Keys to implementations.

Creates several bindings by default:

Author:
crazybob@google.com (Bob Lee)

Nested Class Summary
 class ContainerBuilder.BindingBuilder<T>
          Binds a Key to an implementation in a given scope.
 class ContainerBuilder.ConstantBindingBuilder
          Builds a constant binding.
 class ContainerBuilder.LinkedBindingBuilder<T>
          Links one binding to another.
 
Constructor Summary
ContainerBuilder()
          Constructs a new builder for a development environment (see Stage.DEVELOPMENT).
ContainerBuilder(Stage stage)
          Constructs a new builder.
 
Method Summary
<T> ContainerBuilder.BindingBuilder<T>
bind(Class<T> clazz)
          Binds the given type.
<T> ContainerBuilder.BindingBuilder<T>
bind(Key<T> key)
          Binds the given key.
<T> ContainerBuilder.BindingBuilder<T>
bind(TypeLiteral<T> typeLiteral)
          Binds the given type.
 ContainerBuilder.ConstantBindingBuilder bindConstant(Annotation annotation)
          Binds a constant to the given annotation.
 ContainerBuilder.ConstantBindingBuilder bindConstant(Class<? extends Annotation> annotationType)
          Binds a constant to the given annotation type.
 Container create()
          Creates a Container instance.
 void install(Module module)
          Applies the given module to this builder.
 void intercept(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)
          Applies the given method interceptor to the methods matched by the class and method matchers.
<T> ContainerBuilder.LinkedBindingBuilder<T>
link(Key<T> key)
          Links the given key to another key effectively creating an alias for a binding.
 void requestStaticInjection(Class<?>... types)
          Upon successful creation, the Container will inject static fields and methods in the given classes.
 void scope(Class<? extends Annotation> annotationType, Scope scope)
          Adds a new scope.
 
Methods inherited from class com.google.inject.spi.SourceConsumer
getSourceProvider, setSourceProvider, source, withSourceProvider
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ContainerBuilder

public ContainerBuilder(Stage stage)
Constructs a new builder.

Parameters:
stage - we're running in. If the stage is Stage.PRODUCTION, we will eagerly load container-scoped objects.

ContainerBuilder

public ContainerBuilder()
Constructs a new builder for a development environment (see Stage.DEVELOPMENT).

Method Detail

intercept

public void intercept(Matcher<? super Class<?>> classMatcher,
                      Matcher<? super Method> methodMatcher,
                      MethodInterceptor... interceptors)
Applies the given method interceptor to the methods matched by the class and method matchers.

Parameters:
classMatcher - matches classes the interceptor should apply to. For example: only(Runnable.class).
methodMatcher - matches methods the interceptor should apply to. For example: annotatedWith(Transactional.class).
interceptors - to apply

scope

public void scope(Class<? extends Annotation> annotationType,
                  Scope scope)
Adds a new scope. Maps a Scope instance to a given annotation.


bind

public <T> ContainerBuilder.BindingBuilder<T> bind(Key<T> key)
Binds the given key.


bind

public <T> ContainerBuilder.BindingBuilder<T> bind(TypeLiteral<T> typeLiteral)
Binds the given type.


bind

public <T> ContainerBuilder.BindingBuilder<T> bind(Class<T> clazz)
Binds the given type.


link

public <T> ContainerBuilder.LinkedBindingBuilder<T> link(Key<T> key)
Links the given key to another key effectively creating an alias for a binding.


bindConstant

public ContainerBuilder.ConstantBindingBuilder bindConstant(Annotation annotation)
Binds a constant to the given annotation.


bindConstant

public ContainerBuilder.ConstantBindingBuilder bindConstant(Class<? extends Annotation> annotationType)
Binds a constant to the given annotation type.


requestStaticInjection

public void requestStaticInjection(Class<?>... types)
Upon successful creation, the Container will inject static fields and methods in the given classes.

Parameters:
types - for which static members will be injected

install

public void install(Module module)
Applies the given module to this builder.


create

public Container create()
                 throws ContainerCreationException
Creates a Container instance. Injects static members for classes which were registered using requestStaticInjection(Class...).

Throws:
ContainerCreationException - if configuration errors are found. The expectation is that the application will log this exception and exit.
IllegalStateException - if called more than once