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);
| Modifier and Type | Field and Description |
|---|---|
| private PrivateBinder | binder
Like abstract module, the binder of the current private module |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| protected final void | |
| protected final void | |
| protected final void | |
| protected final <T> LinkedBindingBuilder | |
| protected final <T> AnnotatedBindingBuilder | |
| protected final <T> AnnotatedBindingBuilder | |
| protected final AnnotatedConstantBindingBuilder | |
| protected PrivateBinder | |
| protected final void | bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)
|
| protected void | |
| protected void | |
| protected final void | |
| public final synchronized void | configure(Binder binder)
Implements com. binder.
|
| protected abstract void | |
| protected final void | |
| protected final Stage | |
| protected final <T> void | |
| protected final AnnotatedElementBuilder | |
| protected final AnnotatedElementBuilder | |
| protected <T> MembersInjector | |
| protected <T> MembersInjector | |
| protected final <T> Provider | |
| protected final <T> Provider | |
| protected final void | |
| protected final void | |
| protected final void | |
| protected final void | |
| protected final void |
| binder | back to summary |
|---|---|
| private PrivateBinder binder Like abstract module, the binder of the current private module | |
| PrivateModule | back to summary |
|---|---|
| public PrivateModule() | |
| addError | back to summary |
|---|---|
protected final void addError(String message, Object... arguments)
| |
| addError | back to summary |
|---|---|
protected final void addError(Throwable t)
| |
| addError | back to summary |
|---|---|
protected final void addError(Message message)
| |
| bind | back to summary |
|---|---|
protected final <T> LinkedBindingBuilder
| |
| bind | back to summary |
|---|---|
protected final <T> AnnotatedBindingBuilder
| |
| bind | back to summary |
|---|---|
protected final <T> AnnotatedBindingBuilder
| |
| bindConstant | back to summary |
|---|---|
protected final AnnotatedConstantBindingBuilder bindConstant()
| |
| binder | back to summary |
|---|---|
| protected PrivateBinder binder() Returns the current binder. | |
| bindInterceptor | back to summary |
|---|---|
| protected final void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors) | |
| bindListener | back to summary |
|---|---|
| protected void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener) | |
| bindListener | back to summary |
|---|---|
protected void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners)
| |
| bindScope | back to summary |
|---|---|
protected final void bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope)
| |
| configure | back to summary |
|---|---|
| public final synchronized void configure(Binder binder) Implements com. Doc from com. Contributes bindings and other configurations for this module to Do not invoke this method directly to install submodules. Instead use
| |
| configure | back to summary |
|---|---|
| protected abstract void configure() Creates bindings and other configurations private to this module. Use | |
| convertToTypes | back to summary |
|---|---|
| protected final void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) | |
| currentStage | back to summary |
|---|---|
protected final Stage currentStage()
| |
| expose | back to summary |
|---|---|
| protected final <T> void expose(Key<T> key) Makes the binding for | |
| expose | back to summary |
|---|---|
| protected final AnnotatedElementBuilder expose(Class<?> type) Makes a binding for | |
| expose | back to summary |
|---|---|
| protected final AnnotatedElementBuilder expose(TypeLiteral<?> type) Makes a binding for | |
| getMembersInjector | back to summary |
|---|---|
protected <T> MembersInjector
| |
| getMembersInjector | back to summary |
|---|---|
| protected <T> MembersInjector | |
| getProvider | back to summary |
|---|---|
protected final <T> Provider
| |
| getProvider | back to summary |
|---|---|
protected final <T> Provider
| |
| install | back to summary |
|---|---|
protected final void install(Module module)
| |
| requestInjection | back to summary |
|---|---|
protected final void requestInjection(Object instance)
| |
| requestStaticInjection | back to summary |
|---|---|
| protected final void requestStaticInjection(Class<?>... types) | |
| requireBinding | back to summary |
|---|---|
| protected final void requireBinding(Key<?> key) Instructs Guice to require a binding to the given key. | |
| requireBinding | back to summary |
|---|---|
| protected final void requireBinding(Class<?> type) Instructs Guice to require a binding to the given type. | |