com.google.inject
Class PrivateModule

java.lang.Object
  extended by com.google.inject.PrivateModule
All Implemented Interfaces:
Module

public abstract class PrivateModule
extends Object
implements Module

A module whose configuration information is hidden from its environment by default. Only bindings that are explicitly exposed will be available to other modules and to the users of the injector. This module may expose the bindings it creates and the bindings of the modules it installs.

A private module can be nested within a regular module or within another private module using install(). Its bindings live in a new environment that inherits bindings, type converters, scopes, and interceptors from the surrounding ("parent") environment. When you nest multiple private modules, the result is a tree of environments where the injector's environment is the root.

Guice EDSL bindings can be exposed with expose(). @Provides bindings can be exposed with the @Exposed annotation:

 public class FooBarBazModule extends PrivateModule {
   protected void configurePrivateBindings() {
     bind(Foo.class).to(RealFoo.class);
     expose(Foo.class);

     install(new TransactionalBarModule());
     expose(Bar.class).annotatedWith(Transactional.class);

     bind(SomeImplementationDetail.class);
     install(new MoreImplementationDetailsModule());
   }

   @Provides @Exposed
   public Baz provideBaz() {
     return new SuperBaz();
   }
 }
 

Private modules are implemented using parent injectors. When it can satisfy their dependencies, just-in-time bindings will be created in the root environment. Such bindings are shared among all environments in the tree.

The scope of a binding is constrained to its environment. A singleton bound in a private module will be unique to its environment. But a binding for the same type in a different private module will yield a different instance.

A shared binding that injects the Injector gets the root injector, which only has access to bindings in the root environment. An explicit binding that injects the Injector gets access to all bindings in the child environment.

To promote a just-in-time binding to an explicit binding, bind it:

   bind(FooImpl.class);
 


Constructor Summary
PrivateModule()
           
 
Method Summary
protected  void addError(Message message)
           
protected  void addError(String message, Object... arguments)
           
protected  void addError(Throwable t)
           
protected
<T> AnnotatedBindingBuilder<T>
bind(Class<T> clazz)
           
protected
<T> LinkedBindingBuilder<T>
bind(Key<T> key)
           
protected
<T> AnnotatedBindingBuilder<T>
bind(TypeLiteral<T> typeLiteral)
           
protected  AnnotatedConstantBindingBuilder bindConstant()
           
protected  Binder binder()
           
protected  void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)
           
protected  void bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope)
           
protected  void configure()
           
 void configure(Binder binder)
          Contributes bindings and other configurations for this module to a Binder.
protected abstract  void configurePrivateBindings()
          Creates bindings and other configurations private to this module.
protected  void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter)
           
protected  Stage currentStage()
           
protected  AnnotatedElementBuilder expose(Class<?> type)
          Makes a binding for type available to other modules and the injector.
protected
<T> void
expose(Key<T> key)
          Makes the binding for key available to other modules and the injector.
protected  AnnotatedElementBuilder expose(TypeLiteral<?> type)
          Makes a binding for type available to other modules and the injector.
protected
<T> Provider<T>
getProvider(Class<T> type)
           
protected
<T> Provider<T>
getProvider(Key<T> key)
           
protected  void install(Module module)
           
protected  void requestInjection(Object... objects)
           
protected  void requestStaticInjection(Class<?>... types)
           
protected  void requireBinding(Class<?> type)
           
protected  void requireBinding(Key<?> key)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PrivateModule

public PrivateModule()
Method Detail

configure

public final void configure(Binder binder)
Description copied from interface: Module
Contributes bindings and other configurations for this module to a Binder.

Do not invoke this method directly to install submodules. Instead use Binder.install(Module), which ensures that provider methods are discovered.

Specified by:
configure in interface Module

configurePrivateBindings

protected abstract void configurePrivateBindings()
Creates bindings and other configurations private to this module. Use expose() to make the bindings in this module available externally.


expose

protected final <T> void expose(Key<T> key)
Makes the binding for key available to other modules and the injector.


expose

protected final AnnotatedElementBuilder expose(Class<?> type)
Makes a binding for type available to other modules and the injector. Use annotatedWith() to expose type with a binding annotation.


expose

protected final AnnotatedElementBuilder expose(TypeLiteral<?> type)
Makes a binding for type available to other modules and the injector. Use annotatedWith() to expose type with a binding annotation.


configure

protected final void configure()

binder

protected final Binder binder()

bindScope

protected final void bindScope(Class<? extends Annotation> scopeAnnotation,
                               Scope scope)

bind

protected final <T> LinkedBindingBuilder<T> bind(Key<T> key)

bind

protected final <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral)

bind

protected final <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz)

bindConstant

protected final AnnotatedConstantBindingBuilder bindConstant()

install

protected final void install(Module module)

addError

protected final void addError(String message,
                              Object... arguments)

addError

protected final void addError(Throwable t)

addError

protected final void addError(Message message)

requestInjection

protected final void requestInjection(Object... objects)

requestStaticInjection

protected final void requestStaticInjection(Class<?>... types)

bindInterceptor

protected final void bindInterceptor(Matcher<? super Class<?>> classMatcher,
                                     Matcher<? super Method> methodMatcher,
                                     MethodInterceptor... interceptors)

requireBinding

protected final void requireBinding(Key<?> key)

requireBinding

protected final void requireBinding(Class<?> type)

getProvider

protected final <T> Provider<T> getProvider(Key<T> key)

getProvider

protected final <T> Provider<T> getProvider(Class<T> type)

convertToTypes

protected final void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher,
                                    TypeConverter converter)

currentStage

protected final Stage currentStage()