@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); // Get all currently active CDI managed bean instances in the session scope. Map
The "native" CDI way would otherwise look like this:
// Get the CDI managed bean reference (proxy) of the given bean class. Set<Bean<?>> beans = beanManager.getBeans(SomeBean.class); Bean<SomeBean> bean = (Bean<SomeBean>) beanManager.resolve(beans); CreationalContext<SomeBean> context = beanManager.createCreationalContext(bean); SomeBean someBean = (SomeBean) beanManager.getReference(bean, SomeBean.class, context);
If you need a dependency-free way of obtaining the CDI managed bean instance (e.g. when you want to write code which
should also run on Tomcat), use BeanManager
enum instead.
BeansLocal
Constructor and Description |
---|
Beans() |
Modifier and Type | Method and Description |
---|---|
static <T> void |
destroy(javax.enterprise.inject.spi.Bean<T> bean)
Destroy the currently active instance of the given CDI managed bean representation.
|
static <T> void |
destroy(Class<T> beanClass)
Destroy the currently active instance of the given CDI managed bean class.
|
static <S extends javax.enterprise.context.NormalScope> |
getActiveInstances(Class<S> scope)
Returns all active CDI managed bean instances in the given CDI managed bean scope.
|
static <A extends Annotation> |
getAnnotation(javax.enterprise.inject.spi.Annotated annotated,
Class<A> annotationType)
Get program element annotation of a certain annotation type.
|
static <T> T |
getInstance(javax.enterprise.inject.spi.Bean<T> bean,
boolean create)
Returns the CDI managed bean instance of the given resolved bean from the given bean manager 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)
Returns the CDI managed bean instance (actual) of the given class and creates one if one doesn't exist.
|
static <T> T |
getInstance(Class<T> beanClass,
boolean create)
Returns the CDI managed bean instance (actual) of the given class 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 javax.enterprise.inject.spi.BeanManager |
getManager()
Returns the CDI bean manager.
|
static <T> T |
getReference(javax.enterprise.inject.spi.Bean<T> bean)
Returns the CDI managed bean reference (proxy) of the given bean.
|
static <T> T |
getReference(Class<T> beanClass)
Returns the CDI managed bean reference (proxy) of the given class.
|
static <T> javax.enterprise.inject.spi.Bean<T> |
resolve(Class<T> beanClass)
Returns the CDI managed bean representation of the given bean class.
|
public static javax.enterprise.inject.spi.BeanManager getManager()
public static <T> javax.enterprise.inject.spi.Bean<T> resolve(Class<T> beanClass)
T
- The generic CDI managed bean type.beanClass
- The CDI managed bean class.null
if there is none.public static <T> T getReference(Class<T> beanClass)
T
- The expected return type.beanClass
- The CDI managed bean class.null
if there is none.public static <T> T getReference(javax.enterprise.inject.spi.Bean<T> bean)
T
- The expected return type.bean
- The CDI managed bean representation.null
if there is none.public static <T> T getInstance(Class<T> beanClass)
T
- The expected return type.beanClass
- The CDI managed bean class.null
if there is none.public static <T> T getInstance(Class<T> beanClass, 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.beanClass
- The CDI managed bean class.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
.public static <T> T getInstance(javax.enterprise.inject.spi.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
.public static <S extends javax.enterprise.context.NormalScope> 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
.public static <T> void destroy(Class<T> beanClass)
beanClass
- The CDI managed bean class.public static <T> void destroy(javax.enterprise.inject.spi.Bean<T> bean)
bean
- The CDI managed bean representation.public static <A extends Annotation> A getAnnotation(javax.enterprise.inject.spi.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
.Copyright © 2012–2014 OmniFaces. All rights reserved.