Class InjectionProvider
An InjectionProvider shares the same functionality as the Injector class,
but allows to create multiple instances.
All calls to the Injector class are forwarded to a runtime-wide shared InjectionProvider, called the root provider.
Most applications should use the Injector class instead of this class.
This class is useful if a subsystem needs to provide its own values without interfering with the main application.
- Since:
- 1.9
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the map containing the provided values visible for all threads.default <T> @NotNull Tinject(@NotNull InjectionKey<T> key) Returns the value provided for the specified key.default <T> @NotNull TReturns the value provided for the specified key class.default <T> @NotNull TReturns the value provided for the specified key.<T> @Nullable TinjectOptional(@NotNull InjectionKey<T> key) <T> @Nullable TinjectOptional(@NotNull Class<T> key) <T> @Nullable TinjectOptional(@NotNull String key) local()Creates a new localInjectionEnvironmentfor this InjectionProvider.<T> voidprovide(@NotNull InjectionKey<T> key, @NotNull T value) Provides the specified value for the specified injection key.<T> voidProvides the specified value for the specified key class.voidProvides the specified value for the specified injection key.<T> booleanprovideIfAbsent(@NotNull InjectionKey<T> key, @NotNull T value) Provides the specified value for the specified injection key if the key is not already provided.<T> booleanprovideIfAbsent(@NotNull Class<T> key, @NotNull T value) Provides the specified value for the specified key class if the key class is not already provided.booleanprovideIfAbsent(@NotNull String key, @NotNull Object value) Provides the specified value for the specified injection key if the key is not already provided.voidunprovide(@NotNull InjectionKey<?> key) Removes the specified injection key from this provider.voidRemoves the specified injection from this provider.voidRemoves the specified injection key from this provider.
-
Constructor Details
-
InjectionProvider
public InjectionProvider()Creates a newInjectionProviderwith no values provided.
-
-
Method Details
-
getInjectionMap
-
injectOptional
-
injectOptional
-
injectOptional
-
local
Creates a new localInjectionEnvironmentfor this InjectionProvider. 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 this InjectionProvider directly, the value of the environment will be used for
injectcalls, effectively hiding the provider's value.- Returns:
- The new environment.
-
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.
-
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.
-
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.
-
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 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
-
provide
Provides the specified value for the specified injection key.- 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 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 this provider. If the key is not provided, nothing happens.- Parameters:
key- The injection key.
-
provide
Provides the specified value for the specified key class. 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 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
-