Class BeanManagerProvider
- All Implemented Interfaces:
javax.enterprise.inject.spi.Extension
This class provides access to the BeanManager
by registering the current BeanManager in an extension and
making it available via a singleton factory for the current application.
This is really handy if you like to access CDI functionality from places where no injection is available.
If a simple but manual bean-lookup is needed, it's easier to use the BeanProvider.
As soon as an application shuts down, the reference to the BeanManager will be removed.
Usage:
BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
Attention: This method is intended for being used in user code at runtime.
If this method gets used during Container boot (in an Extension), non-portable
behaviour results. During bootstrapping an Extension shall @Inject BeanManager to get
access to the underlying BeanManager (see e.g. cleanupFinalBeanManagers(javax.enterprise.inject.spi.AfterDeploymentValidation) ).
This is the only way to guarantee to get the right
BeanManager in more complex Container scenarios.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcleanupFinalBeanManagers(javax.enterprise.inject.spi.AfterDeploymentValidation adv) By cleaning the final BeanManager map after the Deployment got Validated, we prevent premature loading of information from JNDI in cases where the container might not be fully setup yet.voidcleanupStoredBeanManagerOnShutdown(javax.enterprise.inject.spi.BeforeShutdown beforeShutdown) Cleanup on container shutdownjavax.enterprise.inject.spi.BeanManagerThe activeBeanManagerfor the current application (/ClassLoader).static ClassLoaderDetect the right ClassLoader.static BeanManagerProviderAllows to get the current provider instance which provides access to the currentBeanManagerstatic booleanisActive()Returns if theBeanManagerProviderhas been initialized.voidsetBeanManager(javax.enterprise.inject.spi.AfterBeanDiscovery afterBeanDiscovery, javax.enterprise.inject.spi.BeanManager beanManager) It basically doesn't matter which of the system events we use, but basically we use theAfterBeanDiscoveryevent since it allows to use theBeanManagerProviderfor all events which occur after theAfterBeanDiscoveryevent.
-
Constructor Details
-
BeanManagerProvider
public BeanManagerProvider()
-
-
Method Details
-
isActive
public static boolean isActive()Returns if theBeanManagerProviderhas been initialized. Usually it isn't needed to call this method in application code. It's e.g. useful for other frameworks to check if DeltaSpike and the CDI container in general have been started.- Returns:
- true if the bean-manager-provider is ready to be used
-
getInstance
Allows to get the current provider instance which provides access to the currentBeanManager- Returns:
- the singleton BeanManagerProvider
- Throws:
IllegalStateException- if theBeanManagerProviderisn't ready to be used. That's the case if the environment isn't configured properly and therefore theAfterBeanDiscoveryhasn't be called before this method gets called.
-
setBeanManager
public void setBeanManager(@Observes javax.enterprise.inject.spi.AfterBeanDiscovery afterBeanDiscovery, javax.enterprise.inject.spi.BeanManager beanManager) It basically doesn't matter which of the system events we use, but basically we use theAfterBeanDiscoveryevent since it allows to use theBeanManagerProviderfor all events which occur after theAfterBeanDiscoveryevent.- Parameters:
afterBeanDiscovery- event which we don't actually use ;)beanManager- the BeanManager we store and make available.
-
getBeanManager
public javax.enterprise.inject.spi.BeanManager getBeanManager()The activeBeanManagerfor the current application (/ClassLoader). This method will throw anIllegalStateExceptionif the BeanManager cannot be found.- Returns:
- the current bean-manager, never
null - Throws:
IllegalStateException- if the BeanManager cannot be found
-
getClassLoader
Detect the right ClassLoader. The lookup order is determined by:- ContextClassLoader of the current Thread
- ClassLoader of the given Object 'o'
- ClassLoader of this very ClassUtils class
- Parameters:
o- if notnullit may get used to detect the classloader.- Returns:
- The
ClassLoaderwhich should get used to create new instances
-
cleanupFinalBeanManagers
public void cleanupFinalBeanManagers(@Observes javax.enterprise.inject.spi.AfterDeploymentValidation adv) By cleaning the final BeanManager map after the Deployment got Validated, we prevent premature loading of information from JNDI in cases where the container might not be fully setup yet. This might happen if someone uses the BeanManagerProvider during Extension startup. -
cleanupStoredBeanManagerOnShutdown
public void cleanupStoredBeanManagerOnShutdown(@Observes javax.enterprise.inject.spi.BeforeShutdown beforeShutdown) Cleanup on container shutdown- Parameters:
beforeShutdown- cdi shutdown event
-