public class InjectorBuilder extends AbstractContext
InjectorBuilder is a Builder implementation that is capable of creating a
simple Injector
. Additionally, it is root Context
to make
configuring the built Injector as easy as possible. Injectors created by
InjectorBuilder instances memoize their created objects.
Internally, it uses an BindingFunctionBuilder
to accumulate
bind rules and a DefaultInjector
to resolve dependencies.
Constructor and Description |
---|
InjectorBuilder(Module... modules)
Deprecated.
use
create(Module...) instead |
Modifier and Type | Method and Description |
---|---|
InjectorBuilder |
applyModule(Module module)
Apply a module to the root context of this InjectorBuilder (i.e.
|
Context |
at(Annotation annot,
Class<?> type)
Create a new Context that extends the current context stack with the given class, qualified
by the specific Annotation instance.
|
Context |
at(Class<?> type)
Create a new Context that extends the current context stack with the given class type as an
anchored match.
|
Context |
at(Class<? extends Annotation> qualifier,
Class<?> type)
Create a new Context that extends the current context stack with the given class and
Qualifier annotation as an anchored match. |
<T> Binding<T> |
bind(Class<T> type)
Start a new binding for the given type T within the scope of this
context.
|
<T> Binding<T> |
bindAny(Class<T> type)
Start a new binding for a type irrespective of qualifier.
|
Injector |
build() |
static InjectorBuilder |
create(ClassLoader loader,
Module... modules)
Create a new injector builder using the specified class loader.
|
static InjectorBuilder |
create(Module... modules)
Create a new injector builder with the default SPI.
|
Context |
matching(ContextPattern pattern)
Restruct context to matching a particular pattern.
|
InjectorBuilder |
setDefaultCachePolicy(CachePolicy policy)
Set the default cache policy used by injectors created by this builder.
|
InjectorBuilder |
setProviderInjectionEnabled(boolean enable)
Set whether or not to enable provider injection support in the built
Injectors.
|
Context |
within(Annotation annot,
Class<?> type)
Create a new Context that extends the current context stack with the
given class, qualified by the specific Annotation instance.
|
Context |
within(Class<?> type)
Create a new Context that extends the current context stack with the
given class type.
|
Context |
within(Class<? extends Annotation> qualifier,
Class<?> type)
Create a new Context that extends the current context stack with the
given class and
Qualifier annotation. |
bind, in, in, in
@Deprecated public InjectorBuilder(Module... modules)
create(Module...)
insteadapplyModule(Module)
. Additional Modules can be applied later
as well. Configuration via the Context
interface is also possible
(and recommended if Modules aren't used) before calling build()
.modules
- Any modules to apply immediatelypublic static InjectorBuilder create(ClassLoader loader, Module... modules)
loader
- The class loader.modules
- The initial modules to configure.public static InjectorBuilder create(Module... modules)
modules
- The initial modules to configure.public InjectorBuilder setDefaultCachePolicy(CachePolicy policy)
policy
- The default policyNullPointerException
- if policy is nullIllegalArgumentException
- if policy is NO_PREFERENCEpublic InjectorBuilder setProviderInjectionEnabled(boolean enable)
enable
- True if the injector should support "provider injection"public <T> Binding<T> bind(Class<T> type)
Context
AllowUnqualifiedMatch
.T
- The matched source typetype
- The raw class that is matchedpublic <T> Binding<T> bindAny(Class<T> type)
Context
bind(type).withAnyQualifier()
.bindAny
in interface Context
bindAny
in class AbstractContext
type
- The type.public Context within(Class<?> type)
Context
Qualifier
. This is equivalent
to within(null, type);
type
- The type to extend this context bypublic Context within(Class<? extends Annotation> qualifier, Class<?> type)
Context
Qualifier
annotation. If the qualifier is null,
the default or null qualifier is used.qualifier
- The qualifier type that must be matched along with the typetype
- The type to extend this context bypublic Context within(Annotation annot, Class<?> type)
Context
The annotation provided must be serializable. Annotations built by AnnotationBuilder
(recommended) or retrieved from the Java
reflection API are serializable; if you use some other annotation implementation, it must be
serializable.
annot
- The qualifier instance that must be matched along with
the typetype
- The type to extend this context bypublic Context matching(ContextPattern pattern)
Context
pattern
- The context pattern to match.public Context at(Class<?> type)
Context
Qualifier
. This is equivalent to
at(null, type);
type
- The type to extend this context byContext.at(Class, Class)
public Context at(Class<? extends Annotation> qualifier, Class<?> type)
Context
Qualifier
annotation as an anchored match. If the qualifier is null, the default or null
qualifier is used.
Unlike Context.in(Class,Class)
, this match is anchored — that is, it only
matches at the end of a context chain. Context is matched if it ends with this or,
if further context is opened inside this context, if the inner context matches immediately.
qualifier
- The qualifier type that must be matched along with the typetype
- The type to extend this context bypublic Context at(Annotation annot, Class<?> type)
Context
The annotation provided must be serializable. Annotations built by AnnotationBuilder
(recommended) or retrieved from the Java
reflection API are serializable; if you use some other annotation implementation, it must be
serializable.
annot
- The qualifier instance that must be matched along with the typetype
- The type to extend this context bypublic InjectorBuilder applyModule(Module module)
Module.configure(Context)
).module
- The module to applypublic Injector build()
Copyright © 2016 GroupLens Research. All rights reserved.