@Typed public final class Beans extends Object
Collection of utility methods for the CDI API that are mainly shortcuts for obtaining stuff from the
BeanManager
.
Some examples:
// Get the CDI managed bean reference (proxy) of the given bean class. SomeBean someBean = Beans.getReference(SomeBean.class);
// Get the CDI managed bean instance (actual) of the given bean class. SomeBean someBean = Beans.getInstance(SomeBean.class);
// Check if CDI session scope is active in current context. Beans.isActive(SessionScope.class);
// Get all currently active CDI managed bean instances in the session scope. Map<Object, String> activeSessionScopedBeans = Beans.getActiveInstances(SessionScope.class);
// Destroy any currently active CDI managed bean instance of given bean class. Beans.destroy(SomeBean.class);
// Fire a CDI event. Beans.fireEvent(someEvent);
Modifier and Type | Method and Description |
---|---|
static <T> void |
destroy(Bean<T> bean)
Destroy the currently active instance of the given CDI managed bean representation.
|
static <T> void |
destroy(Class<T> beanClass,
Annotation... qualifiers)
Destroy the currently active instance of the given CDI managed bean class, optionally with the given qualifiers.
|
static <T> void |
destroy(T instance)
Destroy the currently active instance of the given CDI managed bean instance.
|
static void |
fireEvent(Object event,
Annotation... qualifiers)
Fires the given CDI event, optionally with the given qualifiers.
|
static <S extends Annotation> |
getActiveInstances(Class<S> scope)
Returns all active CDI managed bean instances in the given CDI managed bean scope.
|
static <A extends Annotation> |
getAnnotation(Annotated annotated,
Class<A> annotationType)
Get program element annotation of a certain annotation type.
|
static InjectionPoint |
getCurrentInjectionPoint(CreationalContext<?> creationalContext)
Gets the current injection point when called from a context where injection is taking place (e.g.
|
static <T> T |
getInstance(Bean<T> bean,
boolean create)
Returns the CDI managed bean instance (actual) of the given bean representation and creates one if one doesn't
exist and
create argument is true , otherwise don't create one and return
null if there's no current instance. |
static <T> T |
getInstance(Class<T> beanClass,
Annotation... qualifiers)
Returns the CDI managed bean instance (actual) of the given bean class, optionally with the given qualifiers,
and creates one if one doesn't exist.
|
static <T> T |
getInstance(Class<T> beanClass,
boolean create,
Annotation... qualifiers)
Returns the CDI managed bean instance (actual) of the given bean class, optionally with the given qualifiers,
and creates one if one doesn't exist and
create argument is true , otherwise don't
create one and return null if there's no current instance. |
static BeanManager |
getManager()
Returns the CDI bean manager.
|
static <A extends Annotation> |
getQualifier(InjectionPoint injectionPoint,
Class<A> qualifierClass)
Returns the qualifier annotation of the given qualifier class from the given injection point.
|
static <T> T |
getReference(Bean<T> bean)
Returns the CDI managed bean reference (proxy) of the given bean representation.
|
static <T> T |
getReference(Class<T> beanClass,
Annotation... qualifiers)
Returns the CDI managed bean reference (proxy) of the given bean class, optionally with the given qualifiers.
|
static <S extends Annotation> |
isActive(Class<S> scope)
Returns
true when the given CDI managed bean scope is active. |
static <T> Bean<T> |
resolve(Class<T> beanClass,
Annotation... qualifiers)
Returns the CDI managed bean representation of the given bean class, optionally with the given qualifiers.
|
public static BeanManager getManager()
CDI.getBeanManager()
public static <T> Bean<T> resolve(Class<T> beanClass, Annotation... qualifiers)
T
- The generic CDI managed bean type.beanClass
- The CDI managed bean class.qualifiers
- The CDI managed bean qualifiers, if any.null
if there is none.BeanManager.getBeans(java.lang.reflect.Type, Annotation...)
,
BeanManager.resolve(java.util.Set)
public static <T> T getReference(Class<T> beanClass, Annotation... qualifiers)
T
- The expected return type.beanClass
- The CDI managed bean class.qualifiers
- The CDI managed bean qualifiers, if any.null
if there is none.resolve(Class, Annotation...)
,
getReference(Bean)
,
resolve(Class, Annotation...)
public static <T> T getReference(Bean<T> bean)
T
- The expected return type.bean
- The CDI managed bean representation.null
if there is none.BeanManager.createCreationalContext(javax.enterprise.context.spi.Contextual)
,
BeanManager.getReference(Bean, java.lang.reflect.Type, javax.enterprise.context.spi.CreationalContext)
public static <T> T getInstance(Class<T> beanClass, Annotation... qualifiers)
T
- The expected return type.beanClass
- The CDI managed bean class.qualifiers
- The CDI managed bean qualifiers, if any.getInstance(Class, boolean, Annotation...)
public static <T> T getInstance(Class<T> beanClass, boolean create, Annotation... qualifiers)
create
argument is true
, otherwise don't
create one and return null
if there's no current instance.T
- The expected return type.beanClass
- The CDI managed bean class.create
- Whether to create create CDI managed bean instance if one doesn't exist.qualifiers
- The CDI managed bean qualifiers, if any.null
if there is none
and/or the create
argument is false
.resolve(Class, Annotation...)
,
getInstance(Bean, boolean)
public static <T> T getInstance(Bean<T> bean, boolean create)
create
argument is true
, otherwise don't create one and return
null
if there's no current instance.T
- The expected return type.bean
- The CDI managed bean representation.create
- Whether to create create CDI managed bean instance if one doesn't exist.null
if there is none and/or
the create
argument is false
.BeanManager.getContext(Class)
,
BeanManager.createCreationalContext(javax.enterprise.context.spi.Contextual)
,
Context.get(javax.enterprise.context.spi.Contextual, javax.enterprise.context.spi.CreationalContext)
public static <S extends Annotation> boolean isActive(Class<S> scope)
true
when the given CDI managed bean scope is active. I.e., all beans therein can be
accessed without facing ContextNotActiveException
.S
- The generic CDI managed bean scope type.scope
- The CDI managed bean scope, e.g. SessionScoped.class
.true
when the given CDI managed bean scope is active.BeanManager.getContext(Class)
,
Context.isActive()
public static <S extends Annotation> Map<Object,String> getActiveInstances(Class<S> scope)
S
- The generic CDI managed bean scope type.scope
- The CDI managed bean scope, e.g. RequestScoped.class
.BeanManager.getBeans(java.lang.reflect.Type, Annotation...)
,
BeanManager.getContext(Class)
,
Context.get(javax.enterprise.context.spi.Contextual)
public static <T> void destroy(Class<T> beanClass, Annotation... qualifiers)
T
- The generic CDI managed bean type.beanClass
- The CDI managed bean class.qualifiers
- The CDI managed bean qualifiers, if any.resolve(Class, Annotation...)
,
BeanManager.getContext(Class)
,
AlterableContext.destroy(javax.enterprise.context.spi.Contextual)
public static <T> void destroy(Bean<T> bean)
T
- The generic CDI managed bean type.bean
- The CDI managed bean representation.IllegalArgumentException
- When the given CDI managed bean type is actually not put in an alterable
context.BeanManager.getContext(Class)
,
AlterableContext.destroy(javax.enterprise.context.spi.Contextual)
public static <T> void destroy(T instance)
T
- The generic CDI managed bean type.instance
- The CDI managed bean instance.IllegalArgumentException
- When the given CDI managed bean type is actually not put in an alterable
context.resolve(Class, Annotation...)
,
BeanManager.getContext(Class)
,
AlterableContext.destroy(javax.enterprise.context.spi.Contextual)
public static <A extends Annotation> A getAnnotation(Annotated annotated, Class<A> annotationType)
Annotated.getAnnotation(Class)
is that this method will recursively search inside all Stereotype
annotations.A
- The generic annotation type.annotated
- A Java program element that can be annotated.annotationType
- The class of the annotation type.null
.public static InjectionPoint getCurrentInjectionPoint(CreationalContext<?> creationalContext)
This is mostly intended to be used from within a dynamic producer Bean
. For a "regular" producer (using Produces
)
an InjectionPoint
can either be injected into the bean that contains the producer method, or directly provided as argument
of said method.
creationalContext
- a CreationalContext
used to manage objects with a
Dependent
scopepublic static <A extends Annotation> A getQualifier(InjectionPoint injectionPoint, Class<A> qualifierClass)
A
- The generic annotation type.injectionPoint
- The injection point to obtain the qualifier annotation of the given qualifier class from.qualifierClass
- The class of the qualifier annotation to be looked up in the given injection point.public static void fireEvent(Object event, Annotation... qualifiers)
event
- The event object.qualifiers
- The event qualifiers, if any.BeanManager.fireEvent(Object, Annotation...)
Copyright © 2012–2016 OmniFaces. All rights reserved.