public abstract class

PrivateModule

extends Object
implements Module
java.lang.Object
   ↳ com.google.inject.PrivateModule

Class Overview

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 configure() {
     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);
 

Summary

Public Constructors
PrivateModule()
Public Methods
synchronized final void configure(Binder binder)
Contributes bindings and other configurations for this module to binder.
Protected Methods
final void addError(Message message)
final void addError(String message, Object... arguments)
final void addError(Throwable t)
final <T> LinkedBindingBuilder<T> bind(Key<T> key)
final <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral)
final <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz)
final AnnotatedConstantBindingBuilder bindConstant()
final void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)
void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)
final void bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope)
final PrivateBinder binder()
Returns the current binder.
abstract void configure()
Creates bindings and other configurations private to this module.
final void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter)
final Stage currentStage()
final <T> void expose(Key<T> key)
Makes the binding for key available to other modules and the injector.
final AnnotatedElementBuilder expose(TypeLiteral<?> type)
Makes a binding for type available to other modules and the injector.
final AnnotatedElementBuilder expose(Class<?> type)
Makes a binding for type available to other modules and the injector.
<T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type)
<T> MembersInjector<T> getMembersInjector(Class<T> type)
final <T> Provider<T> getProvider(Key<T> key)
final <T> Provider<T> getProvider(Class<T> type)
final void install(Module module)
final void requestInjection(Object instance)
final void requestStaticInjection(Class...<?> types)
final void requireBinding(Key<?> key)
Instructs Guice to require a binding to the given key.
final void requireBinding(Class<?> type)
Instructs Guice to require a binding to the given type.
[Expand]
Inherited Methods
From class java.lang.Object
From interface com.google.inject.Module

Public Constructors

public PrivateModule ()

Public Methods

public final synchronized void configure (Binder binder)

Contributes bindings and other configurations for this module to binder.

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

Protected Methods

protected final void addError (Message message)

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

protected final void addError (Throwable t)

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

See Also

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

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

See Also

protected final AnnotatedConstantBindingBuilder bindConstant ()

See Also

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

protected void bindListener (Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)

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

protected final PrivateBinder binder ()

Returns the current binder.

protected abstract void configure ()

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

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

protected final Stage currentStage ()

See Also

protected final void expose (Key<T> key)

Makes the binding for key available to other modules and the injector.

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.

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.

protected MembersInjector<T> getMembersInjector (TypeLiteral<T> type)

protected MembersInjector<T> getMembersInjector (Class<T> type)

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

See Also

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

protected final void install (Module module)

See Also

protected final void requestInjection (Object instance)

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

protected final void requireBinding (Key<?> key)

Instructs Guice to require a binding to the given key.

protected final void requireBinding (Class<?> type)

Instructs Guice to require a binding to the given type.