Class BeanProvider
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 Summary
Modifier and TypeMethodDescriptionstatic <T> Set<jakarta.enterprise.inject.spi.Bean<T>> getBeanDefinitions(Class<T> type, boolean optional, boolean includeDefaultScopedBeans) Get a set ofBeandefinitions by type, regardless of qualifiers.static <T> Set<jakarta.enterprise.inject.spi.Bean<T>> getBeanDefinitions(Class<T> type, boolean optional, boolean includeDefaultScopedBeans, jakarta.enterprise.inject.spi.BeanManager beanManager) Get a set ofBeandefinitions by type, regardless of qualifiers.static <T> TgetContextualReference(jakarta.enterprise.inject.spi.BeanManager beanManager, Class<T> type, boolean optional, Annotation... qualifiers) getContextualReference(Class, Annotation...)which returnsnullif the 'optional' parameter is set totrue.static <T> TgetContextualReference(jakarta.enterprise.inject.spi.BeanManager beanManager, String name, boolean optional, Class<T> type) Get a Contextual Reference by its EL Name.static <T> TgetContextualReference(Class<T> type, boolean optional, Annotation... qualifiers) getContextualReference(Class, Annotation...)which returnsnullif the 'optional' parameter is set totrue.static <T> TgetContextualReference(Class<T> type, jakarta.enterprise.inject.spi.Bean<T> bean) Get the Contextual Reference for the given bean.static <T> TgetContextualReference(Class<T> type, Annotation... qualifiers) Get a Contextual Reference by its type and qualifiers.static ObjectgetContextualReference(String name) Get a Contextual Reference by its EL Name.static ObjectgetContextualReference(String name, boolean optional) Get a Contextual Reference by its EL Name.static <T> TgetContextualReference(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> TinjectFields(T instance) Performs dependency injection on an instance.
-
Method Details
-
getContextualReference
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, jakarta.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 questionqualifiers- 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
public static <T> T getContextualReference(Class<T> type, boolean optional, Annotation... qualifiers) getContextualReference(Class, Annotation...)which returnsnullif the 'optional' parameter is set totrue.- Type Parameters:
T- target type- Parameters:
type- the type of the bean in questionoptional- iftrueit will returnnullif no bean could be found or created. Otherwise it will throw anIllegalStateExceptionqualifiers- additional qualifiers which distinguish the resolved bean- Returns:
- the resolved Contextual Reference
- See Also:
-
getContextualReference
public static <T> T getContextualReference(jakarta.enterprise.inject.spi.BeanManager beanManager, Class<T> type, boolean optional, Annotation... qualifiers) getContextualReference(Class, Annotation...)which returnsnullif the 'optional' parameter is set totrue. 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 usetype- the type of the bean in questionoptional- iftrueit will returnnullif no bean could be found or created. Otherwise it will throw anIllegalStateExceptionqualifiers- additional qualifiers which further distinct the resolved bean- Returns:
- the resolved Contextual Reference
- See Also:
-
getContextualReference
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- Returns:
- the resolved Contextual Reference
- Throws:
IllegalStateException- if the bean could not be found.- See Also:
-
getContextualReference
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 beanoptional- iftrueit will returnnullif no bean could be found or created. Otherwise it will throw anIllegalStateException- Returns:
- the resolved Contextual Reference
-
getContextualReference
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 beanoptional- iftrueit will returnnullif no bean could be found or created. Otherwise it will throw anIllegalStateExceptiontype- the type of the bean in question - usegetContextualReference(String, boolean)if the type is unknown e.g. in dyn. use-cases- Returns:
- the resolved Contextual Reference
-
getContextualReference
public static <T> T getContextualReference(jakarta.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 usename- the EL name of the beanoptional- iftrueit will returnnullif no bean could be found or created. Otherwise it will throw anIllegalStateExceptiontype- the type of the bean in question - usegetContextualReference(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, jakarta.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 questionbean- bean definition for the contextual reference- Returns:
- the resolved Contextual Reference
-
getContextualReferences
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
BeanContainer.resolve(java.util.Set)which we cannot use in this case!- Type Parameters:
T- target type- Parameters:
type- the type of the bean in questionoptional- iftrueit will return an empty list if no bean could be found or created. Otherwise it will throw anIllegalStateException- 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 atgetContextualReferences(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
BeanContainer.resolve(java.util.Set)which we cannot use in this case!- Type Parameters:
T- target type- Parameters:
type- the type of the bean in questionoptional- iftrueit will return an empty list if no bean could be found or created. Otherwise it will throw anIllegalStateExceptionincludeDefaultScopedBeans- 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<jakarta.enterprise.inject.spi.Bean<T>> getBeanDefinitions(Class<T> type, boolean optional, boolean includeDefaultScopedBeans) Get a set ofBeandefinitions by type, regardless of qualifiers.- Type Parameters:
T- target type- Parameters:
type- the type of the bean in questionoptional- iftrueit will return an empty set if no bean could be found. Otherwise it will throw anIllegalStateExceptionincludeDefaultScopedBeans- specifies whether dependent scoped beans should be included in the result- Returns:
- the resolved set of
Beandefinitions or an empty set if optional is true
-
getBeanDefinitions
public static <T> Set<jakarta.enterprise.inject.spi.Bean<T>> getBeanDefinitions(Class<T> type, boolean optional, boolean includeDefaultScopedBeans, jakarta.enterprise.inject.spi.BeanManager beanManager) Get a set ofBeandefinitions by type, regardless of qualifiers.- Type Parameters:
T- target type- Parameters:
type- the type of the bean in questionoptional- iftrueit will return an empty set if no bean could be found. Otherwise it will throw anIllegalStateExceptionincludeDefaultScopedBeans- specifies whether dependent scoped beans should be included in the resultbeanManager- theBeanManagerto use- Returns:
- the resolved set of
Beandefinitions 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)
-