Class PrivateModule
- All Implemented Interfaces:
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);
- Since:
- 2.0
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected void
protected void
protected void
protected <T> AnnotatedBindingBuilder<T>
protected <T> LinkedBindingBuilder<T>
protected <T> AnnotatedBindingBuilder<T>
bind(TypeLiteral<T> typeLiteral)
protected PrivateBinder
binder()
Returns the current binder.protected abstract void
Creates bindings and other configurations private to this module.void
Contributes bindings and other configurations for this module tobinder
.protected AnnotatedElementBuilder
Makes a binding fortype
available to other modules and the injector.protected <T> void
Makes the binding forkey
available to other modules and the injector.protected AnnotatedElementBuilder
expose(TypeLiteral<?> type)
Makes a binding fortype
available to other modules and the injector.protected <T> MembersInjector<T>
getMembersInjector(Class<T> type)
protected <T> MembersInjector<T>
getMembersInjector(TypeLiteral<T> type)
protected <T> Provider<T>
getProvider(Class<T> type)
protected <T> Provider<T>
getProvider(Key<T> key)
protected void
-
Constructor Details
-
PrivateModule
public PrivateModule()
-
-
Method Details
-
configure
Description copied from interface:Module
Contributes bindings and other configurations for this module tobinder
.Do not invoke this method directly to install submodules. Instead use
Binder.install(Module)
, which ensures thatprovider methods
are discovered. -
configure
protected abstract void configure()Creates bindings and other configurations private to this module. Useexpose()
to make the bindings in this module available externally. -
expose
Makes the binding forkey
available to other modules and the injector. -
expose
Makes a binding fortype
available to other modules and the injector. UseannotatedWith()
to exposetype
with a binding annotation. -
expose
Makes a binding fortype
available to other modules and the injector. UseannotatedWith()
to exposetype
with a binding annotation. -
binder
Returns the current binder. -
bind
- See Also:
Binder.bind(Key)
-
bind
- See Also:
Binder.bind(TypeLiteral)
-
bind
- See Also:
Binder.bind(Class)
-
install
- See Also:
Binder.install(Module)
-
addError
- See Also:
Binder.addError(String, Object[])
-
addError
- See Also:
Binder.addError(Throwable)
-
addError
- See Also:
Binder.addError(Message)
-
getProvider
- See Also:
Binder.getProvider(Key)
-
getProvider
- See Also:
Binder.getProvider(Class)
-
getMembersInjector
- See Also:
Binder.getMembersInjector(Class)
-
getMembersInjector
- See Also:
Binder.getMembersInjector(TypeLiteral)
-