Class Injector
- All Implemented Interfaces:
ResourceLocator
It stores a trie of binding graphs and a cache of already made singletons.
Each injector is associated with exactly zero or one instance per Key.
Injector uses binding graph at the root of the trie to recursively create and then store instances of objects
associated with some keys.
Branches of the trie are used to enter scopes.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionstatic InjectorThis constructor threadsafelycompilesgiven module, extracting bindings and their multibinders, transformers and generators from it, with no instance overridesstatic Injectorcompile(@Nullable Injector parent, Scope[] scope, Trie<Scope, Map<Key<?>, Set<Binding<?>>>> bindingsMultimap, Multibinder<?> multibinder, BindingTransformer<?> transformer, BindingGenerator<?> generator) The most full-fledged compile method that allows you to create an Injector of any configuration.voidenterScope(Scope scope) Creates an injector that operates on a binding graph at a given prefix (scope) of the binding graph trie and this injector as its parent.@Nullable Binding<?>getBinding(Key<?> key) @Nullable Binding<?>getBinding(Class<?> type) This method returns bindings for current scopeThis method returns a trie of bindings<T> TgetInstance(Key<T> key) Returns an instance for given key.<T> InstanceInjector<T>getInstanceInjector(Key<T> key) A shortcut forgetInstance(new Key<InstanceInjector<T>>(){})<T> InstanceInjector<T>getInstanceInjector(Class<T> type) A shortcut forgetInstanceInjector(Key.of(type))<T> TgetInstanceOrNull(Key<T> key) Same asgetInstance(Key)except that it returnsnullinstead of throwing an exception.<T> InstanceProvider<T>getInstanceProvider(Key<T> key) A shortcut forgetInstance(new Key<InstanceProvider<T>>(){})<T> InstanceProvider<T>getInstanceProvider(Class<T> type) A shortcut forgetInstanceProvider(Key.of(type))<T> OptionalDependency<T>getOptionalDependency(Key<T> key) A shortcut forgetInstance(new Key<OptionalDependency<T>>(){})<T> OptionalDependency<T>getOptionalDependency(Class<T> type) A shortcut forgetOptionalDependency(Key.of(type))@Nullable InjectorScope[]getScope()booleanhasBinding(Key<?> key) This method returns true if a binding was bound for given key.booleanhasBinding(Class<?> type) This method returns true if a binding was bound for given class key.booleanhasInstance(Key<?> key) This method checks if an instance for this key was created by agetInstance(io.activej.inject.Key<T>)call before.booleanhasInstance(Class<?> type) This method checks if an instance for this key was created by agetInstance(io.activej.inject.Key<T>)call before.static Injectorstatic InjectorThis constructor combines given modules and thencompilesthem.static InjectorThis constructor is a shortcut for threadsafecompilewith no instance overrides and no multibinders, transformers or generators.<T> TpeekInstance(Key<T> key) This method returns an instance only if it already was created by agetInstance(io.activej.inject.Key<T>)call before, it does not trigger instance creation.<T> TpeekInstance(Class<T> type) This method returns an instance only if it already was created by agetInstance(io.activej.inject.Key<T>)call before, it does not trigger instance creation.This method returns a copy of the injector cache - a map of all already created non-transient instances at the current scope.<T> voidputInstance(Key<T> key, T instance) This method puts an instance into a cache slot of given key, meaning that already existing instance would be replaced or a binding would never be actually called.<T> voidputInstance(Class<T> key, T instance) This method puts an instance into a cache slot of given key, meaning that already existing instance would be replaced or a binding would never be actually called.toString()static voidEnables specialization of compiled bindings.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface io.activej.inject.ResourceLocator
getInstance, getInstanceOr, getInstanceOr, getInstanceOrCreate, getInstanceOrCreate, getInstanceOrNull
-
Method Details
-
useSpecializer
public static void useSpecializer()Enables specialization of compiled bindings. Depends onActiveJ-Specializermodule -
of
This constructor combines given modules and thencompilesthem. -
of
-
of
This constructor is a shortcut for threadsafecompilewith no instance overrides and no multibinders, transformers or generators. -
compile
This constructor threadsafelycompilesgiven module, extracting bindings and their multibinders, transformers and generators from it, with no instance overrides -
compile
public static Injector compile(@Nullable @Nullable Injector parent, Scope[] scope, Trie<Scope, Map<Key<?>, Set<Binding<?>>>> bindingsMultimap, Multibinder<?> multibinder, BindingTransformer<?> transformer, BindingGenerator<?> generator) The most full-fledged compile method that allows you to create an Injector of any configuration.Note that any injector always sets a binding of Injector key to provide itself.
- Parameters:
parent- parent injector that is called when this injector cannot fulfill the requestscope- the scope of the injector, can be described as 'prefix of the root' of the binding trie, used whenentering scopesbindingsMultimap- a trie of binding set graph with multiple possible conflicting bindings per key that are resolved as part of the compilation.multibinder- a multibinder that is called on every binding conflict (seeMultibinders.combinedMultibinder(java.util.Map<io.activej.inject.Key<?>, io.activej.inject.binding.Multibinder<?>>))transformer- a transformer that is called on every binding once (seeBindingTransformers.combinedTransformer(java.util.Map<io.activej.inject.KeyPattern<?>, java.util.Set<io.activej.inject.binding.BindingTransformer<?>>>))generator- a generator that is called on every missing binding (seeBindingGenerators.combinedGenerator(java.util.Map<io.activej.inject.KeyPattern<?>, java.util.Set<io.activej.inject.binding.BindingGenerator<?>>>))- See Also:
-
getInstance
Returns an instance for given key. At first call an instance is created once for non-transient bindings and next calls to this method will return the same instance.This method throws an exception if a binding was not bound for given key, or if a binding refused to make an instance for some reason (returned
null).- Specified by:
getInstancein interfaceResourceLocator
-
getInstanceOrNull
Same asgetInstance(Key)except that it returnsnullinstead of throwing an exception.- Specified by:
getInstanceOrNullin interfaceResourceLocator
-
getInstanceProvider
A shortcut forgetInstance(new Key<InstanceProvider<T>>(){}) -
getInstanceProvider
A shortcut forgetInstanceProvider(Key.of(type))- See Also:
-
getInstanceInjector
A shortcut forgetInstance(new Key<InstanceInjector<T>>(){}) -
getInstanceInjector
A shortcut forgetInstanceInjector(Key.of(type))- See Also:
-
getOptionalDependency
A shortcut forgetInstance(new Key<OptionalDependency<T>>(){}) -
getOptionalDependency
A shortcut forgetOptionalDependency(Key.of(type))- See Also:
-
createEagerInstances
public void createEagerInstances() -
peekInstance
This method returns an instance only if it already was created by agetInstance(io.activej.inject.Key<T>)call before, it does not trigger instance creation. -
peekInstance
This method returns an instance only if it already was created by agetInstance(io.activej.inject.Key<T>)call before, it does not trigger instance creation.- See Also:
-
hasInstance
This method checks if an instance for this key was created by agetInstance(io.activej.inject.Key<T>)call before. -
hasInstance
This method checks if an instance for this key was created by agetInstance(io.activej.inject.Key<T>)call before.- See Also:
-
peekInstances
This method returns a copy of the injector cache - a map of all already created non-transient instances at the current scope. -
putInstance
This method puts an instance into a cache slot of given key, meaning that already existing instance would be replaced or a binding would never be actually called.Use this at your own risk, this allows high control over the injector, but can be easily abused.
-
putInstance
This method puts an instance into a cache slot of given key, meaning that already existing instance would be replaced or a binding would never be actually called.Use this at your own risk, this allows high control over the injector, but can be easily abused.
- See Also:
-
getBinding
-
getBinding
-
hasBinding
This method returns true if a binding was bound for given key. -
hasBinding
This method returns true if a binding was bound for given class key.- See Also:
-
enterScope
Creates an injector that operates on a binding graph at a given prefix (scope) of the binding graph trie and this injector as its parent. -
getParent
-
getScope
-
getBindings
This method returns bindings for current scopeNote that this method expensive to call repeatedly
-
getBindingsTrie
This method returns a trie of bindingsNote that this method expensive to call repeatedly
-
toString
-