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();
}
}
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);
- Since:
- 2.0
- Author:
- jessewilson@google.com (Jesse Wilson)
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidprotected final voidprotected final voidprotected final <T> LinkedBindingBuilder<T> protected final <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral) protected final <T> AnnotatedBindingBuilder<T> protected final AnnotatedConstantBindingBuilderprotected PrivateBinderbinder()Returns the current binder.protected final voidbindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, org.aopalliance.intercept.MethodInterceptor... interceptors) protected voidbindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners) protected voidbindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener) protected final voidbindScope(Class<? extends Annotation> scopeAnnotation, Scope scope) protected abstract voidCreates bindings and other configurations private to this module.final voidContributes bindings and other configurations for this module tobinder.protected final voidconvertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) protected final Stageprotected final <T> voidMakes the binding forkeyavailable to other modules and the injector.protected final AnnotatedElementBuilderexpose(TypeLiteral<?> type) Makes a binding fortypeavailable to other modules and the injector.protected final AnnotatedElementBuilderMakes a binding fortypeavailable to other modules and the injector.protected <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type) protected <T> MembersInjector<T> getMembersInjector(Class<T> type) protected final <T> Provider<T> getProvider(Key<T> key) protected final <T> Provider<T> getProvider(Class<T> type) protected final voidprotected final voidrequestInjection(Object instance) protected final voidrequestStaticInjection(Class<?>... types) protected final voidrequireBinding(Key<?> key) Instructs Guice to require a binding to the given key.protected final voidrequireBinding(Class<?> type) Instructs Guice to require a binding to the given type.
-
Constructor Details
-
PrivateModule
public PrivateModule()
-
-
Method Details
-
configure
Description copied from interface:ModuleContributes 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 methodsare 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 forkeyavailable to other modules and the injector. -
expose
Makes a binding fortypeavailable to other modules and the injector. UseannotatedWith()to exposetypewith a binding annotation. -
expose
Makes a binding fortypeavailable to other modules and the injector. UseannotatedWith()to exposetypewith a binding annotation. -
binder
Returns the current binder. -
bindScope
- See Also:
-
bind
- See Also:
-
bind
- See Also:
-
bind
- See Also:
-
bindConstant
- See Also:
-
install
- See Also:
-
addError
- See Also:
-
addError
- See Also:
-
addError
- See Also:
-
requestInjection
- See Also:
-
requestStaticInjection
- See Also:
-
bindInterceptor
protected final void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, org.aopalliance.intercept.MethodInterceptor... interceptors) - See Also:
-
requireBinding
Instructs Guice to require a binding to the given key. -
requireBinding
Instructs Guice to require a binding to the given type. -
getProvider
- See Also:
-
getProvider
- See Also:
-
convertToTypes
protected final void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter) - See Also:
-
currentStage
- See Also:
-
getMembersInjector
- See Also:
-
getMembersInjector
- See Also:
-
bindListener
- See Also:
-
bindListener
protected void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners) - Since:
- 4.0
- See Also:
-