Interface InjectionManager

All Known Implementing Classes:
CdiSeInjectionManager, DelayedHk2InjectionManager, ImmediateHk2InjectionManager

public interface InjectionManager
Interface provides the communication API between Jersey and Dependency Injection provider.

Lifecycle methods should be called in this order:

  • completeRegistration() - notifies that Jersey bootstrap has been finished and DI provider should be ready for a runtime.
  • shutdown() - Jersey application has been closed and DI provider should make needed cleaning steps.

All getInstance methods can be called after completeRegistration() method has been called because at this all components are bound to injection manager and ready for getting. In turn, shutdown() method stops the possibility to use these methods and closes InjectionManager.

Author:
Petr Bouda
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Completes InjectionManager and the underlying DI provider.
    <T> T
    create(Class<T> createMe)
    Creates an object with the given class.
    <T> T
    Creates, injects and post-constructs an object with the given class.
    Creates and registers the descriptor in the underlying DI provider and returns ForeignDescriptor that is specific descriptor for the underlying DI provider.
    <T> List<T>
    getAllInstances(Type contractOrImpl)
    Gets all services from this injection manager that implement this contract or have this implementation.
    getAllServiceHolders(Class<T> contractOrImpl, Annotation... qualifiers)
    Gets all services from this injection manager that implements this contract or has this implementation along with information about the service which can be kept by ServiceHolder.
    <T> T
    getInstance(Class<T> contractOrImpl)
    Gets the best service from this injection manager that implements this contract or has this implementation.
    <T> T
    getInstance(Class<T> contractOrImpl, Annotation... qualifiers)
    Gets the best service from this injection manager that implements this contract or has this implementation.
    <T> T
    getInstance(Class<T> contractOrImpl, String classAnalyzer)
    Gets the best service from this injection manager that implements this contract or has this implementation.
    <T> T
    getInstance(Type contractOrImpl)
    Gets the best service from this injection manager that implements this contract or has this implementation.
    getInstance(ForeignDescriptor foreignDescriptor)
    Gets the service instance according to ForeignDescriptor which is specific to the underlying DI provider.
    void
    inject(Object injectMe)
    Analyzes the given object and inject into its fields and methods.
    void
    inject(Object injectMe, String classAnalyzer)
    This will analyze the given object and inject into its fields and methods.
    boolean
    isRegistrable(Class<?> clazz)
    Tests whether the provided clazz can be registered by the implementation of the InjectionManager.
    boolean
    Returns true when the InjectionManager has been shutdown, false otherwise.
    void
    preDestroy(Object preDestroyMe)
    Analyzes the given object and call the preDestroy method.
    void
    register(Iterable<Binding> descriptors)
    Registers a collection of beans represented using fields in the provided descriptors.
    void
    register(Object provider)
    Registers a provider.
    void
    register(Binder binder)
    Registers beans which are included in Binder.
    void
    register(Binding binding)
    Registers one bean represented using fields in the provided descriptor.
    void
    Shuts down the entire InjectionManager and the underlying DI provider.
  • Method Details

    • completeRegistration

      void completeRegistration()
      Completes InjectionManager and the underlying DI provider. All registered components are bound to injection manager and after an invocation of this method all components are available using e.g. getInstance(Class).
    • shutdown

      void shutdown()
      Shuts down the entire InjectionManager and the underlying DI provider.

      Shutdown phase is dedicated to make some final cleaning steps regarding underlying DI provider.

    • isShutdown

      boolean isShutdown()
      Returns true when the InjectionManager has been shutdown, false otherwise.
      Returns:
      Whether the InjectionManager has been shutdown.
    • register

      void register(Binding binding)
      Registers one bean represented using fields in the provided descriptor. The final bean can be direct bean or factory object which will create the bean at the time of injection. InjectionManager is able to register a bean represented by a class or direct instance.
      Parameters:
      binding - one descriptor.
      See Also:
    • register

      void register(Iterable<Binding> descriptors)
      Registers a collection of beans represented using fields in the provided descriptors. The final bean can be direct bean or factory object which will create the bean at the time of injection. InjectionManager is able to register a bean represented by a class or direct instance.
      Parameters:
      descriptors - collection of descriptors.
      See Also:
    • register

      void register(Binder binder)
      Registers beans which are included in Binder. Binder can contains all descriptors extending Binding or other binders which are installed together in tree-structure. This method will get all descriptors bound in the given binder and register them in the order how the binders are installed together. In the tree structure, the deeper on the left side will be processed first.
      Parameters:
      binder - collection of descriptors.
      See Also:
    • register

      void register(Object provider) throws IllegalArgumentException
      Registers a provider. An implementation of the InjectionManager should test whether the type of the object can be registered using the method isRegistrable(Class). Then a caller has an certainty that the instance of the tested class can be registered in InjectionManager. If InjectionManager is not able to register the provider then IllegalArgumentException is thrown.
      Parameters:
      provider - object that can be registered in InjectionManager.
      Throws:
      IllegalArgumentException - provider cannot be registered.
    • isRegistrable

      boolean isRegistrable(Class<?> clazz)
      Tests whether the provided clazz can be registered by the implementation of the InjectionManager.
      Parameters:
      clazz - type that is tested whether is registrable by the implementation of InjectionManager.
      Returns:
      true if the InjectionManager is able to register this type.
    • create

      <T> T create(Class<T> createMe)
      Creates an object with the given class.

      The object created is not managed by the injection manager.

      Parameters:
      createMe - The non-null class to create this object from;
      Returns:
      An instance of the object that has been created.
      Since:
      2.35
    • createAndInitialize

      <T> T createAndInitialize(Class<T> createMe)
      Creates, injects and post-constructs an object with the given class. This is equivalent to calling the create-class method followed by the inject-class method followed by the post-construct method.

      The object created is not managed by the injection manager.

      Parameters:
      createMe - The non-null class to create this object from;
      Returns:
      An instance of the object that has been created, injected and post constructed.
    • getAllServiceHolders

      <T> List<ServiceHolder<T>> getAllServiceHolders(Class<T> contractOrImpl, Annotation... qualifiers)
      Gets all services from this injection manager that implements this contract or has this implementation along with information about the service which can be kept by ServiceHolder.
      Type Parameters:
      T - Instance type.
      Parameters:
      contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of.
      qualifiers - The set of qualifiers that must match this service definition.
      Returns:
      An instance of the contract or impl along with other information. May return null if there is no provider that provides the given implementation or contract.
    • getInstance

      <T> T getInstance(Class<T> contractOrImpl, Annotation... qualifiers)
      Gets the best service from this injection manager that implements this contract or has this implementation.

      Use this method only if other information is not needed otherwise use, otherwise use getAllServiceHolders(Class, Annotation...).

      Type Parameters:
      T - Instance type.
      Parameters:
      contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of.
      qualifiers - The set of qualifiers that must match this service definition.
      Returns:
      An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract.
    • getInstance

      <T> T getInstance(Class<T> contractOrImpl, String classAnalyzer)
      Gets the best service from this injection manager that implements this contract or has this implementation.

      Use this method only if other information is not needed otherwise use, otherwise use getAllServiceHolders(Class, Annotation...).

      Type Parameters:
      T - Instance type.
      Parameters:
      contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of.
      classAnalyzer - -------
      Returns:
      An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract.
    • getInstance

      <T> T getInstance(Class<T> contractOrImpl)
      Gets the best service from this injection manager that implements this contract or has this implementation.

      Use this method only if other information is not needed otherwise use, otherwise use getAllServiceHolders(Class, Annotation...).

      Type Parameters:
      T - Instance type.
      Parameters:
      contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of.
      Returns:
      An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract.
    • getInstance

      <T> T getInstance(Type contractOrImpl)
      Gets the best service from this injection manager that implements this contract or has this implementation.

      Use this method only if other information is not needed otherwise use, otherwise use getAllServiceHolders(Class, Annotation...).

      Type Parameters:
      T - Instance type.
      Parameters:
      contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of.
      Returns:
      An instance of the contract or impl. May return null if there is no provider that provides the given implementation or contract.
    • getInstance

      Object getInstance(ForeignDescriptor foreignDescriptor)
      Gets the service instance according to ForeignDescriptor which is specific to the underlying DI provider.
      Parameters:
      foreignDescriptor - DI specific descriptor.
      Returns:
      service instance according to foreign descriptor.
    • createForeignDescriptor

      ForeignDescriptor createForeignDescriptor(Binding binding)
      Creates and registers the descriptor in the underlying DI provider and returns ForeignDescriptor that is specific descriptor for the underlying DI provider.
      Parameters:
      binding - jersey descriptor.
      Returns:
      specific foreign descriptor of the underlying DI provider.
    • getAllInstances

      <T> List<T> getAllInstances(Type contractOrImpl)
      Gets all services from this injection manager that implement this contract or have this implementation.

      Use this method only if other information is not needed otherwise use, otherwise use getAllServiceHolders(Class, Annotation...).

      Type Parameters:
      T - Instance type.
      Parameters:
      contractOrImpl - May not be null, and is the contract or concrete implementation to get the best instance of.
      Returns:
      A list of services implementing this contract or concrete implementation. May not return null, but may return an empty list
    • inject

      void inject(Object injectMe)
      Analyzes the given object and inject into its fields and methods. The object injected in this way will not be managed by HK2
      Parameters:
      injectMe - The object to be analyzed and injected into
    • inject

      void inject(Object injectMe, String classAnalyzer)
      This will analyze the given object and inject into its fields and methods. The object injected in this way will not be managed by HK2
      Parameters:
      injectMe - The object to be analyzed and injected into
    • preDestroy

      void preDestroy(Object preDestroyMe)
      Analyzes the given object and call the preDestroy method. The object given will not be managed by bean manager.
      Parameters:
      preDestroyMe - The object to preDestroy