Class BeanProvider


  • @Typed
    public final class BeanProvider
    extends Object
    A modified copy of org.apache.deltaspike.core.api.provider.BeanProvider. This class contains utility methods for resolution of contextual references in situations where no injection is available because the current class is not managed by the CDI Container. This can happen in e.g. a JPA 2.0 EntityListener, a ServletFilter, a Spring managed Bean, etc.

    Attention: This approach is intended for use in user code at runtime. If BeanProvider is used during Container boot (in an Extension), non-portable behaviour results. The CDI specification only allows injection of the BeanManager during CDI container boot time.

    • Method Detail

      • getContextualReference

        public static <T> T getContextualReference​(Class<T> type,
                                                   Annotation... qualifiers)
        Get a Contextual Reference by its type and qualifiers. You can use this method to get contextual references of a given type. A "Contextual Reference" is a proxy which will automatically resolve the correct contextual instance when you access any method.

        Attention: You shall not use this method to manually resolve a @Dependent bean! The reason is that contextual instances usually live in the well-defined lifecycle of their injection point (the bean they got injected into). But if we manually resolve a @Dependent bean, then it does not belong to such well defined lifecycle (because @Dependent is not @NormalScoped) and thus will not be automatically destroyed at the end of the lifecycle. You need to manually destroy this contextual instance via Contextual.destroy(Object, javax.enterprise.context.spi.CreationalContext). Thus you also need to manually store the CreationalContext and the Bean you used to create the contextual instance.

        Type Parameters:
        T - target type
        Parameters:
        type - the type of the bean in question
        qualifiers - additional qualifiers which further distinct the resolved bean
        Returns:
        the resolved Contextual Reference
        Throws:
        IllegalStateException - if the bean could not be found.
        See Also:
        getContextualReference(Class, boolean, Annotation...)
      • getContextualReference

        public static <T> T getContextualReference​(Class<T> type,
                                                   boolean optional,
                                                   Annotation... qualifiers)
        getContextualReference(Class, Annotation...) which returns null if the 'optional' parameter is set to true.
        Type Parameters:
        T - target type
        Parameters:
        type - the type of the bean in question
        optional - if true it will return null if no bean could be found or created. Otherwise it will throw an IllegalStateException
        qualifiers - additional qualifiers which distinguish the resolved bean
        Returns:
        the resolved Contextual Reference
        See Also:
        getContextualReference(Class, Annotation...)
      • getContextualReference

        public static <T> T getContextualReference​(javax.enterprise.inject.spi.BeanManager beanManager,
                                                   Class<T> type,
                                                   boolean optional,
                                                   Annotation... qualifiers)
        getContextualReference(Class, Annotation...) which returns null if the 'optional' parameter is set to true. This method is intended for usage where the BeanManger is known, e.g. in Extensions.
        Type Parameters:
        T - target type
        Parameters:
        beanManager - the BeanManager to use
        type - the type of the bean in question
        optional - if true it will return null if no bean could be found or created. Otherwise it will throw an IllegalStateException
        qualifiers - additional qualifiers which further distinct the resolved bean
        Returns:
        the resolved Contextual Reference
        See Also:
        getContextualReference(Class, Annotation...)
      • getContextualReference

        public static Object getContextualReference​(String name,
                                                    boolean optional)
        Get a Contextual Reference by its EL Name. This only works for beans with the @Named annotation.

        Attention: please see the notes on manually resolving @Dependent beans in getContextualReference(Class, java.lang.annotation.Annotation...)!

        Parameters:
        name - the EL name of the bean
        optional - if true it will return null if no bean could be found or created. Otherwise it will throw an IllegalStateException
        Returns:
        the resolved Contextual Reference
      • getContextualReference

        public static <T> T getContextualReference​(String name,
                                                   boolean optional,
                                                   Class<T> type)
        Get a Contextual Reference by its EL Name. This only works for beans with the @Named annotation.

        Attention: please see the notes on manually resolving @Dependent beans in getContextualReference(Class, java.lang.annotation.Annotation...)!

        Type Parameters:
        T - target type
        Parameters:
        name - the EL name of the bean
        optional - if true it will return null if no bean could be found or created. Otherwise it will throw an IllegalStateException
        type - the type of the bean in question - use getContextualReference(String, boolean) if the type is unknown e.g. in dyn. use-cases
        Returns:
        the resolved Contextual Reference
      • getContextualReference

        public static <T> T getContextualReference​(javax.enterprise.inject.spi.BeanManager beanManager,
                                                   String name,
                                                   boolean optional,
                                                   Class<T> type)

        Get a Contextual Reference by its EL Name. This only works for beans with the @Named annotation.

        Attention: please see the notes on manually resolving @Dependent bean in getContextualReference(Class, boolean, java.lang.annotation.Annotation...)!

        Type Parameters:
        T - target type
        Parameters:
        beanManager - the BeanManager to use
        name - the EL name of the bean
        optional - if true it will return null if no bean could be found or created. Otherwise it will throw an IllegalStateException
        type - the type of the bean in question - use getContextualReference(String, boolean) if the type is unknown e.g. in dyn. use-cases
        Returns:
        the resolved Contextual Reference
      • getContextualReference

        public static <T> T getContextualReference​(Class<T> type,
                                                   javax.enterprise.inject.spi.Bean<T> bean)
        Get the Contextual Reference for the given bean.

        Attention: please see the notes on manually resolving @Dependent beans in getContextualReference(Class, java.lang.annotation.Annotation...)!

        Type Parameters:
        T - target type
        Parameters:
        type - the type of the bean in question
        bean - bean definition for the contextual reference
        Returns:
        the resolved Contextual Reference
      • getContextualReferences

        public static <T> List<T> getContextualReferences​(Class<T> type,
                                                          boolean optional)
        Get a list of Contextual References by type, regardless of qualifiers (including dependent scoped beans). You can use this method to get all contextual references of a given type. A 'Contextual Reference' is a proxy which will automatically resolve the correct contextual instance when you access any method.

        Attention: please see the notes on manually resolving @Dependent beans in getContextualReference(Class, java.lang.annotation.Annotation...)!

        Attention: This will also return instances of beans for which an Alternative exists! The @Alternative resolving is only done via BeanManager.resolve(java.util.Set) which we cannot use in this case!

        Type Parameters:
        T - target type
        Parameters:
        type - the type of the bean in question
        optional - if true it will return an empty list if no bean could be found or created. Otherwise it will throw an IllegalStateException
        Returns:
        the resolved list of Contextual Reference or an empty-list if optional is true
      • getContextualReferences

        public static <T> List<T> getContextualReferences​(Class<T> type,
                                                          boolean optional,
                                                          boolean includeDefaultScopedBeans)
        Get a list of Contextual References by type, regardless of the qualifier. Further details are available at getContextualReferences(Class, boolean).

        Attention: please see the notes on manually resolving @Dependent bean in getContextualReference(Class, java.lang.annotation.Annotation...)!

        Attention: This will also return instances of beans for which an Alternative exists! The @Alternative resolving is only done via BeanManager.resolve(java.util.Set) which we cannot use in this case!

        Type Parameters:
        T - target type
        Parameters:
        type - the type of the bean in question
        optional - if true it will return an empty list if no bean could be found or created. Otherwise it will throw an IllegalStateException
        includeDefaultScopedBeans - specifies if dependent scoped beans should be included in the result
        Returns:
        the resolved list of Contextual Reference or an empty-list if optional is true
      • getBeanDefinitions

        public static <T> Set<javax.enterprise.inject.spi.Bean<T>> getBeanDefinitions​(Class<T> type,
                                                                                      boolean optional,
                                                                                      boolean includeDefaultScopedBeans)
        Get a set of Bean definitions by type, regardless of qualifiers.
        Type Parameters:
        T - target type
        Parameters:
        type - the type of the bean in question
        optional - if true it will return an empty set if no bean could be found. Otherwise it will throw an IllegalStateException
        includeDefaultScopedBeans - specifies whether dependent scoped beans should be included in the result
        Returns:
        the resolved set of Bean definitions or an empty set if optional is true
      • getBeanDefinitions

        public static <T> Set<javax.enterprise.inject.spi.Bean<T>> getBeanDefinitions​(Class<T> type,
                                                                                      boolean optional,
                                                                                      boolean includeDefaultScopedBeans,
                                                                                      javax.enterprise.inject.spi.BeanManager beanManager)
        Get a set of Bean definitions by type, regardless of qualifiers.
        Type Parameters:
        T - target type
        Parameters:
        type - the type of the bean in question
        optional - if true it will return an empty set if no bean could be found. Otherwise it will throw an IllegalStateException
        includeDefaultScopedBeans - specifies whether dependent scoped beans should be included in the result
        beanManager - the BeanManager to use
        Returns:
        the resolved set of Bean definitions or an empty set if optional is true
      • injectFields

        public static <T> T injectFields​(T instance)
        Performs dependency injection on an instance. Useful for instances which aren't managed by CDI.

        Attention:
        The resulting instance isn't managed by CDI; only fields annotated with @Inject get initialized.

        Type Parameters:
        T - current type
        Parameters:
        instance - current instance
        Returns:
        instance with injected fields (if possible - or null if the given instance is null)