public abstract class PrivateModule extends java.lang.Object implements 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(); } }
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 message) |
protected void |
addError(java.lang.String message,
java.lang.Object... arguments) |
protected void |
addError(java.lang.Throwable t) |
protected <T> AnnotatedBindingBuilder<T> |
bind(java.lang.Class<T> clazz) |
protected <T> LinkedBindingBuilder<T> |
bind(Key<T> key) |
protected <T> AnnotatedBindingBuilder<T> |
bind(TypeLiteral<T> typeLiteral) |
protected PrivateBinder |
binder()
Returns the current binder.
|
protected abstract void |
configure()
Creates bindings and other configurations private to this module.
|
void |
configure(Binder binder)
Contributes bindings and other configurations for this module to
binder . |
protected AnnotatedElementBuilder |
expose(java.lang.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> MembersInjector<T> |
getMembersInjector(java.lang.Class<T> type) |
protected <T> MembersInjector<T> |
getMembersInjector(TypeLiteral<T> type) |
protected <T> Provider<T> |
getProvider(java.lang.Class<T> type) |
protected <T> Provider<T> |
getProvider(Key<T> key) |
protected void |
install(Module module) |
public final void configure(Binder binder)
Module
binder
.
Do not invoke this method directly to install submodules. Instead use
Binder.install(Module)
, 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 AnnotatedElementBuilder expose(java.lang.Class<?> type)
type
available to other modules and the injector. Use annotatedWith()
to expose type
with a
binding annotation.protected final AnnotatedElementBuilder expose(TypeLiteral<?> type)
type
available to other modules and the injector. Use annotatedWith()
to expose type
with a
binding annotation.protected final PrivateBinder binder()
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(java.lang.Class<T> clazz)
Binder.bind(Class)
protected final void install(Module module)
Binder.install(Module)
protected final void addError(java.lang.String message, java.lang.Object... arguments)
Binder.addError(String, Object[])
protected final void addError(java.lang.Throwable t)
Binder.addError(Throwable)
protected final void addError(Message message)
Binder.addError(Message)
protected final <T> Provider<T> getProvider(Key<T> key)
Binder.getProvider(Key)
protected final <T> Provider<T> getProvider(java.lang.Class<T> type)
Binder.getProvider(Class)
protected <T> MembersInjector<T> getMembersInjector(java.lang.Class<T> type)
Binder.getMembersInjector(Class)
protected <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type)
Binder.getMembersInjector(TypeLiteral)