public enum InjectorHolder extends Enum<InjectorHolder>
A thread-safe singleton holding the Guice Injector instance that other classes can call to get to use dependency injection.
The InjectorHolder should be used sparingly, ideally only to be used at the entry points into the system, i.e. Servlets, Filters, etc, and allow Guice at this entry points to construct all of the required dependencies. If introducing into an existing system then as the code is migrated over to be "Guice enabled" form a logical "boundary" in your code which is the entry point(s) into the Guice "world" and at this entry points call the InjectorHolder in the constructors of the boundary entry points to construct its dependencies.
As more of the codebase is migrated to use Guice then this logical boundary will expand and the use of the InjectorHolder should be moved further out with the boundary.
Injector
Enum Constant and Description |
---|
INSTANCE
The Singleton instance of the InjectorHolder.
|
Modifier and Type | Method and Description |
---|---|
static com.google.inject.Injector |
createChildInjector(com.google.inject.Module... modules)
Returns a new injector that inherits all state from this injector.
|
static <T> T |
getInstance(Class<T> clazz)
Uses the Guice injector to return the appropriate instance for the given injection type.
|
static <T> T |
getInstance(com.google.inject.Key<T> key)
Uses the Guice injector to return the appropriate instance for the given injection key.
|
static void |
injectMembers(Object instance)
Injects the dependencies of an already constructed instance.
|
static InjectorHolder |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static InjectorHolder[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final InjectorHolder INSTANCE
public static InjectorHolder[] values()
for (InjectorHolder c : InjectorHolder.values()) System.out.println(c);
public static InjectorHolder valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic static <T> T getInstance(Class<T> clazz)
Uses the Guice injector to return the appropriate instance for the given injection type.
Avoid using this method, in favour of having Guice inject your dependencies ahead of time.
T
- The type of class to get.clazz
- The class to get an instance of.public static <T> T getInstance(com.google.inject.Key<T> key)
Uses the Guice injector to return the appropriate instance for the given injection key.
Avoid using this method, in favour of having Guice inject your dependencies ahead of time.
T
- The type of class defined by the key.key
- The key that defines the class to get.public static void injectMembers(Object instance)
Injects the dependencies of an already constructed instance. This method can be used to inter-operate with objects created by other frameworks or services.
Injects dependencies into the fields and methods of instance
. Ignores the presence or absence of an
injectable constructor.
Preferably let Guice create all your objects for you and you'll never need to use this method.
instance
- A non-null instance to inject members on.public static com.google.inject.Injector createChildInjector(com.google.inject.Module... modules)
Returns a new injector that inherits all state from this injector. All bindings, scopes, interceptors and type converters are inherited -- they are visible to the child injector. Elements of the child injector are not visible to its parent.
Just-in-time bindings created for child injectors will be created in an ancestor injector whenever possible. This allows for scoped instances to be shared between injectors. Use explicit bindings to prevent bindings from being shared with the parent injector.
No key may be bound by both an injector and one of its ancestors. This includes just-in-time
bindings. The lone exception is the key for Injector.class
, which is bound by each
injector to itself.
modules
- An array of Guice modules to use to configure the child injector.Copyright © 2025 Open Identity Platform Community. All rights reserved.