Top Description Fields Constructors Methods
com.google.inject

public abstract Class PrivateModule

extends Object
implements Module
Class Inheritance
All Implemented Interfaces
com.google.inject.Module
Static Imports
com.google.common.base.Preconditions.checkState

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. See the note in createChildInjector about how hierarchical injectors change the importance of otherwise unnecessary binding statements (such as bind(ServiceImpl.class);).

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);
Author
[email protected] (Jesse Wilson)
Since
2.0

Field Summary

Modifier and TypeField and Description
private PrivateBinder
binder

Like abstract module, the binder of the current private module

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected final void
addError(String message, Object... arguments)

protected final void
protected final void
addError(Message message)

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

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

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

protected final AnnotatedConstantBindingBuilder
protected PrivateBinder
binder()

Returns the current binder.

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

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

protected void
bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners)

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

public final synchronized void
configure(Binder binder)

Implements com.google.inject.Module.configure.

Contributes bindings and other configurations for this module to binder.
protected abstract void
configure()

Creates bindings and other configurations private to this module.

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

protected final Stage
protected final <T> void
expose(Key<T> key)

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

protected final AnnotatedElementBuilder
expose(Class<?> type)

Makes a binding for type 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.

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

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

protected final void
install(Module module)

protected final void
protected final void
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.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

binderback to summary
private PrivateBinder binder

Like abstract module, the binder of the current private module

Constructor Detail

PrivateModuleback to summary
public PrivateModule()

Method Detail

addErrorback to summary
protected final void addError(String message, Object... arguments)
See Also
Binder#addError(String, Object[])
addErrorback to summary
protected final void addError(Throwable t)
See Also
Binder#addError(Throwable)
addErrorback to summary
protected final void addError(Message message)
See Also
Binder#addError(Message)
bindback to summary
protected final <T> LinkedBindingBuilder<T> bind(Key<T> key)
See Also
Binder#bind(Key)
bindback to summary
protected final <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral)
See Also
Binder#bind(TypeLiteral)
bindback to summary
protected final <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz)
See Also
Binder#bind(Class)
bindConstantback to summary
protected final AnnotatedConstantBindingBuilder bindConstant()
See Also
Binder#bindConstant()
binderback to summary
protected PrivateBinder binder()

Returns the current binder.

bindInterceptorback to summary
protected final void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)
See Also
Binder#bindInterceptor(com.google.inject.matcher.Matcher, com.google.inject.matcher.Matcher, org.aopalliance.intercept.MethodInterceptor[])
bindListenerback to summary
protected void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)
See Also
Binder#bindListener(com.google.inject.matcher.Matcher, com.google.inject.spi.TypeListener)
bindListenerback to summary
protected void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners)
Since
4.0
See Also
Binder#bindListener(Matcher, ProvisionListener...)
bindScopeback to summary
protected final void bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope)
See Also
Binder#bindScope(Class, Scope)
configureback to summary
public final synchronized void configure(Binder binder)

Implements com.google.inject.Module.configure.

Doc from com.google.inject.Module.configure.

Contributes bindings and other configurations for this module to binder.

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

Annotations
@Override
configureback to summary
protected abstract void configure()

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

convertToTypesback to summary
protected final void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter)
See Also
Binder#convertToTypes(com.google.inject.matcher.Matcher, com.google.inject.spi.TypeConverter)
currentStageback to summary
protected final Stage currentStage()
See Also
Binder#currentStage()
exposeback to summary
protected final <T> void expose(Key<T> key)

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

exposeback to summary
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.

exposeback to summary
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.

getMembersInjectorback to summary
protected <T> MembersInjector<T> getMembersInjector(Class<T> type)
See Also
Binder#getMembersInjector(Class)
getMembersInjectorback to summary
protected <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type)
See Also
Binder#getMembersInjector(TypeLiteral)
getProviderback to summary
protected final <T> Provider<T> getProvider(Key<T> key)
See Also
Binder#getProvider(Key)
getProviderback to summary
protected final <T> Provider<T> getProvider(Class<T> type)
See Also
Binder#getProvider(Class)
installback to summary
protected final void install(Module module)
See Also
Binder#install(Module)
requestInjectionback to summary
protected final void requestInjection(Object instance)
See Also
Binder#requestInjection(Object)
requestStaticInjectionback to summary
protected final void requestStaticInjection(Class<?>... types)
See Also
Binder#requestStaticInjection(Class[])
requireBindingback to summary
protected final void requireBinding(Key<?> key)

Instructs Guice to require a binding to the given key.

requireBindingback to summary
protected final void requireBinding(Class<?> type)

Instructs Guice to require a binding to the given type.