public abstract class PrivateModule extends Objectimplements Module
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);
| Constructor and Description |
|---|
PrivateModule()
|
| Modifier and Type | Method and Description |
|---|---|
protected void |
addError(Message
|
protected void |
addError(String
|
protected void |
addError(Throwable
|
protected <T> AnnotatedBindingBuilder |
bind(Class
|
protected <T> LinkedBindingBuilder |
bind(Key
|
protected <T> AnnotatedBindingBuilder |
bind(TypeLiteral
|
protected AnnotatedConstantBindingBuilder |
bindConstant()
|
protected PrivateBinder |
binder()
Returns the current binder.
|
protected void |
bindInterceptor(Matcher
|
protected void |
bindListener(Matcher
|
protected void |
bindListener(Matcher
|
protected void |
bindScope(Class
|
protected abstract void |
configure()
Creates bindings and other configurations private to this module.
|
void |
configure(Binder
Contributes bindings and other configurations for this module to
binder.
|
protected void |
convertToTypes(Matcher
|
protected Stage |
currentStage()
|
protected AnnotatedElementBuilder |
expose(Class
Makes a binding for
type available to other modules and the injector.
|
protected <T> void |
expose(Key
Makes the binding for
key available to other modules and the injector.
|
protected AnnotatedElementBuilder |
expose(TypeLiteral
Makes a binding for
type available to other modules and the injector.
|
protected <T> MembersInjector |
getMembersInjector(Class
|
protected <T> MembersInjector |
getMembersInjector(TypeLiteral
|
protected <T> Provider |
getProvider(Class
|
protected <T> Provider |
getProvider(Key
|
protected void |
install(Module
|
protected void |
requestInjection(Object
|
protected void |
requestStaticInjection(Class
|
protected void |
requireBinding(Class
Instructs Guice to require a binding to the given type.
|
protected void |
requireBinding(Key
Instructs Guice to require a binding to the given key.
|
public final void configure(Binderbinder)
Module
binder.
Do not invoke this method directly to install submodules. Instead use Binder, which ensures that provider methods are discovered.
protected abstract void configure()
expose() to make the bindings in this module available externally.
protected final <T> void expose(Key<T> key)
key available to other modules and the injector.
protected final AnnotatedElementBuilderexpose(Class <?> type)
type available to other modules and the injector. Use
annotatedWith() to expose
type with a binding annotation.
protected final AnnotatedElementBuilderexpose(TypeLiteral <?> type)
type available to other modules and the injector. Use
annotatedWith() to expose
type with a binding annotation.
protected final PrivateBinderbinder()
protected final void bindScope(Class<? extends Annotation > scopeAnnotation, Scope scope)
Binder.bindScope(Class, Scope)
protected final <T> LinkedBindingBuilder<T> bind(Key <T> key)
Binder.bind(Key)
protected final <T> AnnotatedBindingBuilder<T> bind(TypeLiteral <T> typeLiteral)
Binder.bind(TypeLiteral)
protected final <T> AnnotatedBindingBuilder<T> bind(Class <T> clazz)
Binder.bind(Class)
protected final AnnotatedConstantBindingBuilderbindConstant()
Binder.bindConstant()
protected final void install(Modulemodule)
Binder.install(Module)
protected final void addError(Stringmessage, Object ... arguments)
Binder.addError(String, Object[])
protected final void addError(Throwablet)
Binder.addError(Throwable)
protected final void addError(Messagemessage)
Binder.addError(Message)
protected final void requestInjection(Objectinstance)
Binder.requestInjection(Object)
protected final void requestStaticInjection(Class<?>... types)
Binder.requestStaticInjection(Class[])
protected final void bindInterceptor(Matcher<? super Class <?>> classMatcher, Matcher <? super Method > methodMatcher, org .aopalliance .intercept .MethodInterceptor ... interceptors)
protected final void requireBinding(Key<?> key)
protected final void requireBinding(Class<?> type)
protected final <T> Provider<T> getProvider(Key <T> key)
Binder.getProvider(Key)
protected final <T> Provider<T> getProvider(Class <T> type)
Binder.getProvider(Class)
protected final void convertToTypes(Matcher<? super TypeLiteral <?>> typeMatcher, TypeConverter converter)
protected final StagecurrentStage()
Binder.currentStage()
protected <T> MembersInjector<T> getMembersInjector(Class <T> type)
Binder.getMembersInjector(Class)
protected <T> MembersInjector<T> getMembersInjector(TypeLiteral <T> type)
Binder.getMembersInjector(TypeLiteral)
protected void bindListener(Matcher<? super TypeLiteral <?>> typeMatcher, TypeListener listener)
protected void bindListener(Matcher<? super Binding <?>> bindingMatcher, ProvisionListener ... listeners)
Binder.bindListener(Matcher, ProvisionListener...)