@Typed public final class BeanProvider extends Object
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.
DependentProvider
,
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, regardless of qualifiers. |
static <T> T |
getContextualReference(javax.enterprise.inject.spi.BeanManager beanManager,
Class<T> type,
boolean optional,
java.lang.annotation.Annotation... qualifiers)
getContextualReference(Class, Annotation...) which returns null if the 'optional' parameter
is set to true . |
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.
|
static <T> T |
getContextualReference(Class<T> type,
java.lang.annotation.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,
java.lang.annotation.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, regardless of qualifiers (including dependent scoped beans).
|
static <T> List<T> |
getContextualReferences(Class<T> type,
boolean optional,
boolean includeDefaultScopedBeans)
Get a list of Contextual References by type, regardless of the qualifier.
|
static <T> DependentProvider<T> |
getDependent(javax.enterprise.inject.spi.BeanManager beanManager,
Class<T> type,
java.lang.annotation.Annotation... qualifiers) |
static <T> DependentProvider<T> |
getDependent(javax.enterprise.inject.spi.BeanManager beanManager,
String name) |
static <T> DependentProvider<T> |
getDependent(Class<T> type,
java.lang.annotation.Annotation... qualifiers) |
static <T> DependentProvider<T> |
getDependent(String name) |
static <T> T |
injectFields(T instance)
Performs dependency injection on an instance.
|
public static <T> T getContextualReference(Class<T> type, java.lang.annotation.Annotation... qualifiers)
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.
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, java.lang.annotation.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 IllegalStateException
qualifiers
- additional qualifiers which distinguish the resolved beangetContextualReference(Class, Annotation...)
public static <T> T getContextualReference(javax.enterprise.inject.spi.BeanManager beanManager, Class<T> type, boolean optional, java.lang.annotation.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 IllegalStateException
qualifiers
- additional qualifiers which further distinct the resolved beangetContextualReference(Class, Annotation...)
public static Object getContextualReference(String name)
Attention: please see the notes on manually resolving @Dependent beans in
getContextualReference(Class, 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)
Attention: please see the notes on manually resolving @Dependent beans in
getContextualReference(Class, 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 IllegalStateException
public static <T> T getContextualReference(String name, boolean optional, Class<T> type)
Attention: please see the notes on manually resolving @Dependent beans in
getContextualReference(Class, 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 IllegalStateException
type
- 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(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...)
!
T
- target typebeanManager
- the BeanManager to usename
- 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 IllegalStateException
type
- 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)
Attention: please see the notes on manually resolving @Dependent beans in
getContextualReference(Class, java.lang.annotation.Annotation...)
!
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)
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!
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 IllegalStateException
public static <T> List<T> getContextualReferences(Class<T> type, boolean optional, boolean includeDefaultScopedBeans)
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!
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 IllegalStateException
includeDefaultScopedBeans
- specifies if dependent scoped beans should be included in the resultpublic static <T> DependentProvider<T> getDependent(Class<T> type, java.lang.annotation.Annotation... qualifiers)
public static <T> DependentProvider<T> getDependent(javax.enterprise.inject.spi.BeanManager beanManager, Class<T> type, java.lang.annotation.Annotation... qualifiers)
public static <T> DependentProvider<T> getDependent(String name)
public static <T> DependentProvider<T> getDependent(javax.enterprise.inject.spi.BeanManager beanManager, String name)
public static <T> Set<javax.enterprise.inject.spi.Bean<T>> getBeanDefinitions(Class<T> type, boolean optional, boolean includeDefaultScopedBeans)
Bean
definitions by type, regardless of qualifiers.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 IllegalStateException
includeDefaultScopedBeans
- specifies whether 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 © 2015 The Apache Software Foundation. All rights reserved.