public class BeanManagerProvider extends Object implements javax.enterprise.inject.spi.Extension
This class provides access to the BeanManager by registring the current BeanManager in an extension and making it available via a singleton factory
This is really handy if you like to access CDI functionality from places where no injection is available.
Usage:
BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
Constructor and Description |
---|
BeanManagerProvider() |
Modifier and Type | Method and Description |
---|---|
void |
cleanupStoredBeanManagerOnShutdown(javax.enterprise.inject.spi.BeforeShutdown beforeShutdown)
Cleanup on container shutdown
|
javax.enterprise.inject.spi.BeanManager |
getBeanManager()
The active
BeanManager for the current ClassLoader |
<T> T |
getContextualReference(Class<T> type,
Annotation... qualifiers) |
<T> T |
getContextualReference(Class<T> type,
String name)
Get a Contextual Reference by it's EL Name.
|
static BeanManagerProvider |
getInstance()
Singleton accessor
|
static boolean |
isActive()
Returns if the
BeanManagerProvider has been initialized |
void |
setBeanManager(javax.enterprise.inject.spi.AfterBeanDiscovery afterBeanDiscovery,
javax.enterprise.inject.spi.BeanManager beanManager)
It basiscally doesn't matter which of the system events we use,
but basically we
|
void |
setRootBeanManager(javax.enterprise.inject.spi.BeanManager beanManager) |
protected void |
setTestMode() |
public static boolean isActive()
BeanManagerProvider
has been initializedpublic static BeanManagerProvider getInstance()
public javax.enterprise.inject.spi.BeanManager getBeanManager()
BeanManager
for the current ClassLoader
public <T> T getContextualReference(Class<T> type, Annotation... qualifiers)
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 beanpublic <T> T getContextualReference(Class<T> type, String name)
Get a Contextual Reference by it's EL Name. This only works for beans with the @Named annotation.
Attention: please see the notes on manually resolving @Dependent bean
in getContextualReference(Class, java.lang.annotation.Annotation...)
!
T
- target typetype
- the type of the bean in question - only use Object.class if the type is unknown in dyn. use-casesname
- the EL name of the beanpublic void setBeanManager(@Observes javax.enterprise.inject.spi.AfterBeanDiscovery afterBeanDiscovery, javax.enterprise.inject.spi.BeanManager beanManager)
afterBeanDiscovery
- event which we don't actually use ;)beanManager
- the BeanManager we store and make available.public void setRootBeanManager(javax.enterprise.inject.spi.BeanManager beanManager)
public void cleanupStoredBeanManagerOnShutdown(@Observes javax.enterprise.inject.spi.BeforeShutdown beforeShutdown)
beforeShutdown
- cdi shutdown eventprotected void setTestMode()
Copyright © 2010-2014 The Apache Software Foundation. All Rights Reserved.