@Typed public final class BeanProvider extends Object
This class contains utility methods to resolve 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 method is intended for being used in user code at runtime. If this method gets 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.
BeanManagerProvider| Modifier and Type | Method and Description |
|---|---|
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 independent of the qualifier. |
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. |
static <T> T |
getContextualReference(Class<T> type,
Annotation... qualifiers)
Get a Contextual Reference by its type and qualifiers.
|
static <T> T |
getContextualReference(Class<T> type,
javax.enterprise.inject.spi.Bean<T> bean)
Get the Contextual Reference for the given bean.
|
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. |
static Object |
getContextualReference(String name)
Get a Contextual Reference by its EL Name.
|
static Object |
getContextualReference(String name,
boolean optional)
Get a Contextual Reference by its EL Name.
|
static <T> T |
getContextualReference(String name,
boolean optional,
Class<T> type)
Get a Contextual Reference by its EL Name.
|
static <T> List<T> |
getContextualReferences(Class<T> type,
boolean optional)
Get a list of Contextual References by type independent of the qualifier
(including dependent scoped beans).
|
static <T> List<T> |
getContextualReferences(Class<T> type,
boolean optional,
boolean includeDefaultScopedBeans)
Get a list of Contextual References by type independent of the qualifier.
|
static <T> DependentProvider<T> |
getDependent(Class<T> type,
Annotation... qualifiers) |
static <T> DependentProvider<T> |
getDependent(String name) |
static <T> T |
injectFields(T instance)
Allows to perform dependency injection for instances which aren't managed by CDI.
|
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 this contextual instances do 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 a well defined lifecycle (because @Dependent it is not
@NormalScoped) and thus will not automatically be
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 which this method will not provide.
T - target typetype - the type of the bean in questionqualifiers - additional qualifiers which further distinct the resolved beanIllegalStateException - if the bean could not be found.getContextualReference(Class, boolean, Annotation...)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.T - target typetype - the type of the bean in questionoptional - if true it will return null if no bean could be found or created.
Otherwise it will throw an IllegalStateExceptionqualifiers - additional qualifiers which further distinct the resolved beangetContextualReference(Class, Annotation...)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.T - target typebeanManager - the BeanManager to usetype - the type of the bean in questionoptional - if true it will return null if no bean could be found or created.
Otherwise it will throw an IllegalStateExceptionqualifiers - additional qualifiers which further distinct the resolved beangetContextualReference(Class, Annotation...)public static Object getContextualReference(String name)
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...)!
name - the EL name of the beanIllegalStateException - if the bean could not be found.getContextualReference(String, boolean)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 bean
in getContextualReference(Class, boolean, java.lang.annotation.Annotation...)!
name - the EL name of the beanoptional - if true it will return null if no bean could be found or created.
Otherwise it will throw an IllegalStateExceptionpublic 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 bean
in getContextualReference(Class, boolean, java.lang.annotation.Annotation...)!
T - target typename - the EL name of the beanoptional - if true it will return null if no bean could be found or created.
Otherwise it will throw an IllegalStateExceptiontype - the type of the bean in question - use getContextualReference(String, boolean)
if the type is unknown e.g. in dyn. use-casespublic static <T> T getContextualReference(Class<T> type, javax.enterprise.inject.spi.Bean<T> bean)
T - target typetype - the type of the bean in questionbean - bean-definition for the contextual-referencepublic static <T> List<T> getContextualReferences(Class<T> type, boolean optional)
Get a list of Contextual References by type independent of the qualifier (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 bean
in getContextualReference(Class, boolean, 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!
T - target typetype - the type of the bean in questionoptional - if true it will return an empty list if no bean could be found or created.
Otherwise it will throw an IllegalStateExceptionpublic static <T> List<T> getContextualReferences(Class<T> type, boolean optional, boolean includeDefaultScopedBeans)
Get a list of Contextual References by type independent of the qualifier.
Further details are available at getContextualReferences(Class, boolean)
Attention: please see the notes on manually resolving @Dependent bean
in getContextualReference(Class, boolean, 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!
T - target typetype - the type of the bean in questionoptional - if true it will return an empty list if no bean could be found or created.
Otherwise it will throw an IllegalStateExceptionincludeDefaultScopedBeans - specifies if dependent scoped beans should be included in the resultpublic static <T> DependentProvider<T> getDependent(Class<T> type, Annotation... qualifiers)
public static <T> DependentProvider<T> getDependent(String name)
public static <T> Set<javax.enterprise.inject.spi.Bean<T>> getBeanDefinitions(Class<T> type, boolean optional, boolean includeDefaultScopedBeans)
Bean definitions by type independent of the qualifier.T - target typetype - the type of the bean in questionoptional - if true it will return an empty set if no bean could be found.
Otherwise it will throw an IllegalStateExceptionincludeDefaultScopedBeans - specifies if dependent scoped beans should be included in the resultBean definitions or an empty-set if optional is truepublic static <T> T injectFields(T instance)
T - current typeinstance - current instanceCopyright © 2011-2013 The Apache Software Foundation. All Rights Reserved.