Class Injector
InjectionProvider for the current runtime, sometimes called "root provider".
This provider should be used by most applications that do not create individual providers.
Injector is a simple dependency injection framework.
It allows to provide values using the provide(...) methods and inject them using the inject(...) methods.
Values provided to the Injector are stored for an indefinite period of time, and can be accessed globally.
Injector does not perform any kind of component scanning out of the box.
It is possible to provide values temporarily for a single thread
by creating a local InjectionEnvironment using the local() method.
- Since:
- 1.9
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @NotNull Tinject(@NotNull InjectionKey<T> key) Returns the value provided for the specified key.static <T> @NotNull TReturns the value provided for the specified key class.static <T> @NotNull TReturns the value provided for the specified key.static <T> @Nullable TinjectOptional(@NotNull InjectionKey<T> key) Returns the value provided for the specified key if it exists.static <T> @Nullable TinjectOptional(@NotNull Class<T> key) Returns the value provided for the specified key class if it exists.static <T> @Nullable TinjectOptional(@NotNull String key) Returns the value provided for the specified key if it exists.static @NotNull InjectionEnvironmentlocal()Creates a new localInjectionEnvironment.static <T> voidprovide(@NotNull InjectionKey<T> key, @NotNull T value) Provides the specified value for the specified injection key to the root provider.static <T> voidProvides the specified value for the specified key class to the root provider.static voidProvides the specified value for the specified injection key.static <T> booleanprovideIfAbsent(@NotNull InjectionKey<T> key, @NotNull T value) Provides the specified value for the specified injection key to the root provider if the key is not already provided.static <T> booleanprovideIfAbsent(@NotNull Class<T> key, @NotNull T value) Provides the specified value for the specified key class to the root provider if the key class is not already provided.static booleanprovideIfAbsent(@NotNull String key, @NotNull Object value) Provides the specified value for the specified injection key to the root provider if the key is not already provided.static voidunprovide(@NotNull InjectionKey<?> key) Removes the specified injection key from the root provider.static voidRemoves the specified injection from the root provider.static voidRemoves the specified injection key from the root provider.
-
Method Details
-
local
Creates a new localInjectionEnvironment. This environment can be used to provide local injection values.While the environment is open, values provided to the environment will be visible to
injectcalls. The environment is only open for the current thread. That means, values provided to the environment will only be visible toinjectcalls of the current thread. When the environment is closed, it is no longer open, i.e. values provided to the environment will no longer be visible toinjectcalls.If multiple environments are open at the same time in the same thread and provide the same key, the value of the last environment created will be used for
injectcalls.If an environment is open that provides the same key as the global provider, the value of the environment will be used for
injectcalls, effectively hiding the global provider's value.- Returns:
- The new environment.
- See Also:
-
provide
Provides the specified value for the specified injection key.- Parameters:
key- The injection key.value- The value.- Throws:
InjectionException- If the key is already provided.
-
provideIfAbsent
Provides the specified value for the specified injection key to the root provider if the key is not already provided. If the key is already provided, nothing happens.- Parameters:
key- The injection key.value- The value.- Returns:
- true if the value was provided, false otherwise.
-
unprovide
Removes the specified injection key from the root provider. If the key is not provided, nothing happens.It is recommended to create a local environment using
local()instead of providing and unproviding values frequently.- Parameters:
key- The injection key.
-
injectOptional
Returns the value provided for the specified key if it exists.Note that the return value is auto-casted to the requested type. This is an unchecked cast. It must be ensured that the requested type is compatible with the value.
- Type Parameters:
T- The requested type of the value. The return value is auto-casted to this type.- Parameters:
key- The key.- Returns:
- The value provided for the key. If no value was provided for the key,
nullis returned. The return value is auto-casted to the requested type.
-
inject
Returns the value provided for the specified key.Note that the return value is auto-casted to the requested type. This is an unchecked cast. It must be ensured that the requested type is compatible with the value.
- Type Parameters:
T- The requested type of the value. The return value is auto-casted to this type.- Parameters:
key- The key.- Returns:
- The value provided for the key. If no such value exists, an InjectionException is thrown. The return value is auto-casted to the requested type.
- Throws:
InjectionException- If the injection key was not provided.
-
provide
public static <T> void provide(@NotNull InjectionKey<T> key, @NotNull T value) throws InjectionException Provides the specified value for the specified injection key to the root provider.- Type Parameters:
T- The type of values providable for the key.- Parameters:
key- injection key.value- The value.- Throws:
InjectionException- If the key is already provided.
-
provideIfAbsent
Provides the specified value for the specified injection key to the root provider if the key is not already provided. If the key is already provided, nothing happens.- Type Parameters:
T- The type of values providable for the key.- Parameters:
key- injection key.value- The value.- Returns:
- true if the value was provided, false otherwise.
-
unprovide
Removes the specified injection key from the root provider. If the key is not provided, nothing happens.It is recommended to create a local environment using
local()instead of providing and unproviding values frequently.- Parameters:
key- The injection key.
-
injectOptional
Returns the value provided for the specified key if it exists.- Type Parameters:
T- The type of the value provided by the key.- Parameters:
key- The injection key.- Returns:
- The value provided for the key. If no value was provided for the key,
nullis returned.
-
inject
Returns the value provided for the specified key.- Type Parameters:
T- The type of the value provided by the key.- Parameters:
key- The injection key.- Returns:
- The value provided for the key. If no value was provided for the key, an InjectionException is thrown.
- Throws:
InjectionException- If no value was provided for the key.
-
provide
Provides the specified value for the specified key class to the root provider. The value is only provided for the exact key class specified.- Type Parameters:
T- The type of the value.- Parameters:
key- The key class.value- The value to provide.- Throws:
InjectionException- If the specified key class is already provided.
-
provideIfAbsent
Provides the specified value for the specified key class to the root provider if the key class is not already provided. The value is only provided for the exact key class specified.If the key class is already provided, nothing happens.
- Type Parameters:
T- The type of the value.- Parameters:
key- The key class.value- The value to provide.- Returns:
- true if the value was provided, false otherwise.
-
unprovide
Removes the specified injection from the root provider. If the injected value found for the specified key is provided for additional classes in its class hierarchy that are specializations of the specified class, those occurrences are removed as well.If the key class is not provided, nothing happens.
It is recommended to create a local environment using
local()instead of providing and unproviding values frequently.- Parameters:
key- The key class.
-
injectOptional
Returns the value provided for the specified key class if it exists.- Type Parameters:
T- The type of the class and the value provided by the key class.- Parameters:
key- The injection key class.- Returns:
- The value provided for the key. If no value was provided for the key,
nullis returned.
-
inject
Returns the value provided for the specified key class.- Type Parameters:
T- The type of the class and the value provided by the key class.- Parameters:
key- The injection key class.- Returns:
- The value provided for the key. If no value was provided for the key, an InjectionException is thrown.
- Throws:
InjectionException- If no value was provided for the key class.
-