Class PrivateModule
- java.lang.Object
-
- com.google.inject.PrivateModule
-
- All Implemented Interfaces:
Module
public abstract class PrivateModule extends Object implements Module
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(). @Providesbindings can be exposed with the @Exposedannotation: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
Injectorgets the root injector, which only has access to bindings in the root environment. An explicit binding that injects theInjectorgets 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
Constructors Constructor Description PrivateModule()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidaddError(Message message)protected voidaddError(String message, Object... arguments)protected voidaddError(Throwable t)protected <T> LinkedBindingBuilder<T>bind(Key<T> key)protected <T> AnnotatedBindingBuilder<T>bind(TypeLiteral<T> typeLiteral)protected <T> AnnotatedBindingBuilder<T>bind(Class<T> clazz)protected AnnotatedConstantBindingBuilderbindConstant()protected PrivateBinderbinder()Returns the current binder.protected voidbindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)protected voidbindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners)protected voidbindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)protected voidbindScope(Class<? extends Annotation> scopeAnnotation, Scope scope)protected abstract voidconfigure()Creates bindings and other configurations private to this module.voidconfigure(Binder binder)Contributes bindings and other configurations for this module tobinder.protected voidconvertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter)protected StagecurrentStage()protected <T> voidexpose(Key<T> key)Makes the binding forkeyavailable to other modules and the injector.protected AnnotatedElementBuilderexpose(TypeLiteral<?> type)Makes a binding fortypeavailable to other modules and the injector.protected AnnotatedElementBuilderexpose(Class<?> type)Makes 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 <T> Provider<T>getProvider(Key<T> key)protected <T> Provider<T>getProvider(Class<T> type)protected voidinstall(Module module)protected voidrequestInjection(Object instance)protected voidrequestStaticInjection(Class<?>... types)protected voidrequireBinding(Key<?> key)Instructs Guice to require a binding to the given key.protected voidrequireBinding(Class<?> type)Instructs Guice to require a binding to the given type.
-
-
-
Method Detail
-
configure
public final void configure(Binder binder)
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
protected final <T> void expose(Key<T> key)
Makes the binding forkeyavailable to other modules and the injector.
-
expose
protected final AnnotatedElementBuilder expose(Class<?> type)
Makes a binding fortypeavailable to other modules and the injector. UseannotatedWith()to exposetypewith a binding annotation.
-
expose
protected final AnnotatedElementBuilder expose(TypeLiteral<?> type)
Makes a binding fortypeavailable to other modules and the injector. UseannotatedWith()to exposetypewith a binding annotation.
-
binder
protected final PrivateBinder binder()
Returns the current binder.
-
bindScope
protected final void bindScope(Class<? extends Annotation> scopeAnnotation, Scope scope)
- See Also:
Binder.bindScope(Class, Scope)
-
bind
protected final <T> LinkedBindingBuilder<T> bind(Key<T> key)
- See Also:
Binder.bind(Key)
-
bind
protected final <T> AnnotatedBindingBuilder<T> bind(TypeLiteral<T> typeLiteral)
- See Also:
Binder.bind(TypeLiteral)
-
bind
protected final <T> AnnotatedBindingBuilder<T> bind(Class<T> clazz)
- See Also:
Binder.bind(Class)
-
bindConstant
protected final AnnotatedConstantBindingBuilder bindConstant()
- See Also:
Binder.bindConstant()
-
install
protected final void install(Module module)
- See Also:
Binder.install(Module)
-
addError
protected final void addError(String message, Object... arguments)
- See Also:
Binder.addError(String, Object[])
-
addError
protected final void addError(Throwable t)
- See Also:
Binder.addError(Throwable)
-
addError
protected final void addError(Message message)
- See Also:
Binder.addError(Message)
-
requestInjection
protected final void requestInjection(Object instance)
- See Also:
Binder.requestInjection(Object)
-
requestStaticInjection
protected final void requestStaticInjection(Class<?>... types)
- See Also:
Binder.requestStaticInjection(Class[])
-
bindInterceptor
protected final void bindInterceptor(Matcher<? super Class<?>> classMatcher, Matcher<? super Method> methodMatcher, MethodInterceptor... interceptors)
-
requireBinding
protected final void requireBinding(Key<?> key)
Instructs Guice to require a binding to the given key.
-
requireBinding
protected final void requireBinding(Class<?> type)
Instructs Guice to require a binding to the given type.
-
getProvider
protected final <T> Provider<T> getProvider(Key<T> key)
- See Also:
Binder.getProvider(Key)
-
getProvider
protected final <T> Provider<T> getProvider(Class<T> type)
- See Also:
Binder.getProvider(Class)
-
convertToTypes
protected final void convertToTypes(Matcher<? super TypeLiteral<?>> typeMatcher, TypeConverter converter)
-
currentStage
protected final Stage currentStage()
- See Also:
Binder.currentStage()
-
getMembersInjector
protected <T> MembersInjector<T> getMembersInjector(Class<T> type)
- See Also:
Binder.getMembersInjector(Class)
-
getMembersInjector
protected <T> MembersInjector<T> getMembersInjector(TypeLiteral<T> type)
- See Also:
Binder.getMembersInjector(TypeLiteral)
-
bindListener
protected void bindListener(Matcher<? super TypeLiteral<?>> typeMatcher, TypeListener listener)
-
bindListener
protected void bindListener(Matcher<? super Binding<?>> bindingMatcher, ProvisionListener... listeners)
- Since:
- 4.0
- See Also:
Binder.bindListener(Matcher, ProvisionListener...)
-
-