com.google.inject
Class ContainerBuilder

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

public final class ContainerBuilder
extends java.lang.Object

Builds a dependency injection Container. Binds Keys to implementations. For example, 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.
static interface ContainerBuilder.Command
          Implemented by classes which participate in building a container.
 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(TypeToken<T> typeToken)
          Binds the given type.
 ContainerBuilder bindProperties(java.util.Map<java.lang.String,java.lang.String> properties)
          Binds string constants based on the given properties.
 ContainerBuilder bindProperties(java.util.Properties properties)
          Binds string constants based on the given properties.
 Container create(boolean loadSingletons)
          Creates a Container instance.
 ContainerBuilder injectStatics(java.lang.Class<?>... types)
          Upon creation, the Container will inject static fields and methods in the given classes.
<T> ContainerBuilder.LinkedBindingBuilder<T>
link(Key<T> key)
          Links the given key to another key effectively creating an alias for a binding.
protected  java.lang.Object source()
          Creates a source object to be associated with a binding.
 
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

source

protected java.lang.Object source()
Creates a source object to be associated with a binding. Called by default for each binding. The default implementation returns ContainerBuilder's caller's StackTraceElement.

If you plan on manually setting the source (say for example you've implemented an XML configuration), you might override this method and return null to avoid unnecessary overhead.


bind

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


bind

public <T> ContainerBuilder.BindingBuilder<T> bind(TypeToken<T> typeToken)
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 ContainerBuilder bindProperties(java.util.Map<java.lang.String,java.lang.String> properties)
Binds string constants based on the given properties.


bindProperties

public ContainerBuilder bindProperties(java.util.Properties properties)
Binds string constants based on the given properties.


injectStatics

public ContainerBuilder injectStatics(java.lang.Class<?>... types)
Upon creation, the Container will inject static fields and methods in the given classes.

Parameters:
types - for which static members will be injected

create

public Container create(boolean loadSingletons)
Creates a Container instance. Injects static members for classes which were registered using injectStatics(Class...).

Parameters:
loadSingletons - If true, the container will load all singletons now. If false, the container will lazily load singletons. Eager loading is appropriate for production use while lazy loading can speed development.
Throws:
java.lang.IllegalStateException - if called more than once