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. A binding implementation could be anything from a constant value to an object in the HTTP session.

Not safe for concurrent use.

Default bindings include:

Converts constants as needed from String to any primitive type in addition to enum and Class<?>.

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.
 
Method Summary
<T> ContainerBuilder.BindingBuilder<T>
bind(java.lang.Class<T> clazz)
          Binds the given type.
<T> ContainerBuilder.BindingBuilder<T>
bind(Key<T> key)
          Binds the given key.
 ContainerBuilder.ConstantBindingBuilder bind(java.lang.String name)
          Binds a constant to the given name.
<T> ContainerBuilder.BindingBuilder<T>
bind(TypeLiteral<T> typeLiteral)
          Binds the given type.
 void bindProperties(java.util.Map<java.lang.String,java.lang.String> properties)
          Binds a string constant for each property.
 void bindProperties(java.util.Properties properties)
          Binds a string constant for each property.
 Container create(boolean preload)
          Creates a Container instance.
 void install(Module module)
          Applies the given module to this builder.
 void intercept(Query<? super java.lang.Class<?>> classQuery, Query<? super java.lang.reflect.Method> methodQuery, MethodInterceptor... interceptors)
          Applies the given method interceptor to the methods matched by the class and method queries.
<T> ContainerBuilder.LinkedBindingBuilder<T>
link(Key<T> key)
          Links the given key to another key effectively creating an alias for a binding.
 void requestStaticInjection(java.lang.Class<?>... types)
          Upon successful creation, the Container will inject static fields and methods in the given classes.
 void scope(java.lang.String name, 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()
Constructs a new builder.

Method Detail

intercept

public void intercept(Query<? super java.lang.Class<?>> classQuery,
                      Query<? super java.lang.reflect.Method> methodQuery,
                      MethodInterceptor... interceptors)
Applies the given method interceptor to the methods matched by the class and method queries.

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

scope

public void scope(java.lang.String name,
                  Scope scope)
Adds a new scope. Maps a Scope instance to a given scope name. Scopes should be mapped before used in bindings. Scoped.value() references this name.


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(java.lang.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.


bind

public ContainerBuilder.ConstantBindingBuilder bind(java.lang.String name)
Binds a constant to the given name.


bindProperties

public void bindProperties(java.util.Map<java.lang.String,java.lang.String> properties)
Binds a string constant for each property.


bindProperties

public void bindProperties(java.util.Properties properties)
Binds a string constant for each property.


requestStaticInjection

public void requestStaticInjection(java.lang.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(boolean preload)
                 throws ContainerCreationException
Creates a Container instance. Injects static members for classes which were registered using requestStaticInjection(Class...).

Parameters:
preload - If true, the container will load all container-scoped bindings now. If false, the container will lazily load them. Eager loading is appropriate for production use (catch errors early and take any performance hit up front) while lazy loading can speed development.
Throws:
ContainerCreationException - if configuration errors are found. The expectation is that the application will log this exception and exit.
java.lang.IllegalStateException - if called more than once