java.lang.Object | |
↳ | com.google.inject.PrivateModule |
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.
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);
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Contributes bindings and other configurations for this module to
binder . |
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns the current binder.
| |||||||||||
Creates bindings and other configurations private to this module.
| |||||||||||
Makes the binding for
key available to other modules and the injector. | |||||||||||
Makes a binding for
type available to other modules and the injector. | |||||||||||
Makes a binding for
type available to other modules and the injector. | |||||||||||
Instructs Guice to require a binding to the given key.
| |||||||||||
Instructs Guice to require a binding to the given type.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
java.lang.Object
| |||||||||||
From interface
com.google.inject.Module
|
Contributes bindings and other configurations for this module to binder
.
Do not invoke this method directly to install submodules. Instead use
install(Module)
, which ensures that provider methods
are
discovered.
Creates bindings and other configurations private to this module. Use expose()
to make the bindings in this module available externally.
Makes the binding for key
available to other modules and the injector.
Makes a binding for type
available to other modules and the injector. Use annotatedWith()
to expose type
with a
binding annotation.
Makes a binding for type
available to other modules and the injector. Use annotatedWith()
to expose type
with a
binding annotation.
Instructs Guice to require a binding to the given key.
Instructs Guice to require a binding to the given type.