Interface InjectionManager

  • All Known Implementing Classes:
    AbstractHk2InjectionManager, 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.

    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      void completeRegistration()
      Completes InjectionManager and the underlying DI provider.
      <T> T createAndInitialize​(java.lang.Class<T> createMe)
      Creates, injects and post-constructs an object with the given class.
      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.
      <T> java.util.List<T> getAllInstances​(java.lang.reflect.Type contractOrImpl)
      Gets all services from this injection manager that implement this contract or have this implementation.
      <T> java.util.List<ServiceHolder<T>> getAllServiceHolders​(java.lang.Class<T> contractOrImpl, java.lang.annotation.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​(java.lang.Class<T> contractOrImpl)
      Gets the best service from this injection manager that implements this contract or has this implementation.
      <T> T getInstance​(java.lang.Class<T> contractOrImpl, java.lang.annotation.Annotation... qualifiers)
      Gets the best service from this injection manager that implements this contract or has this implementation.
      <T> T getInstance​(java.lang.Class<T> contractOrImpl, java.lang.String classAnalyzer)
      Gets the best service from this injection manager that implements this contract or has this implementation.
      <T> T getInstance​(java.lang.reflect.Type contractOrImpl)
      Gets the best service from this injection manager that implements this contract or has this implementation.
      java.lang.Object getInstance​(ForeignDescriptor foreignDescriptor)
      Gets the service instance according to ForeignDescriptor which is specific to the underlying DI provider.
      void inject​(java.lang.Object injectMe)
      Analyzes the given object and inject into its fields and methods.
      void inject​(java.lang.Object injectMe, java.lang.String classAnalyzer)
      This will analyze the given object and inject into its fields and methods.
      boolean isRegistrable​(java.lang.Class<?> clazz)
      Tests whether the provided clazz can be registered by the implementation of the InjectionManager.
      void preDestroy​(java.lang.Object preDestroyMe)
      Analyzes the given object and call the preDestroy method.
      void register​(java.lang.Iterable<Binding> descriptors)
      Registers a collection of beans represented using fields in the provided descriptors.
      void register​(java.lang.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 shutdown()
      Shuts down the entire InjectionManager and the underlying DI provider.
    • Method Detail

      • 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.

      • 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:
        ClassBinding, InstanceBinding, SupplierClassBinding, SupplierInstanceBinding
      • register

        void register​(java.lang.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:
        ClassBinding, InstanceBinding, SupplierClassBinding, SupplierInstanceBinding
      • 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:
        ClassBinding, InstanceBinding, SupplierClassBinding, SupplierInstanceBinding
      • register

        void register​(java.lang.Object provider)
               throws java.lang.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:
        java.lang.IllegalArgumentException - provider cannot be registered.
      • isRegistrable

        boolean isRegistrable​(java.lang.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.
      • createAndInitialize

        <T> T createAndInitialize​(java.lang.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> java.util.List<ServiceHolder<T>> getAllServiceHolders​(java.lang.Class<T> contractOrImpl,
                                                                  java.lang.annotation.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​(java.lang.Class<T> contractOrImpl,
                          java.lang.annotation.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​(java.lang.Class<T> contractOrImpl,
                          java.lang.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​(java.lang.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​(java.lang.reflect.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

        java.lang.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> java.util.List<T> getAllInstances​(java.lang.reflect.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​(java.lang.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​(java.lang.Object injectMe,
                    java.lang.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​(java.lang.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