Package org.jboss.weld.environment.se
Class WeldContainer
- java.lang.Object
-
- jakarta.enterprise.inject.spi.CDI<T>
-
- org.jboss.weld.AbstractCDI<Object>
-
- org.jboss.weld.environment.se.WeldContainer
-
- All Implemented Interfaces:
Instance<Object>
,SeContainer
,Provider<Object>
,AutoCloseable
,Iterable<Object>
,org.jboss.weld.environment.ContainerInstance
,WeldInstance<Object>
@Vetoed public class WeldContainer extends AbstractCDI<Object> implements AutoCloseable, org.jboss.weld.environment.ContainerInstance, SeContainer
Represents a Weld SE container.An new instance can be initialized using the Weld builder:
WeldContainer container = new Weld().initialize();
It's also possible to obtain the instance of a running container by id:
WeldContainer container = WeldContainer.instance("myCustomId");
shutdown()
must be always called to shutdown the container properly. AutoCloseable is implemented, so the container is automatically shut down when leaving the try-with-resources block:try (WeldContainer container = new Weld().initialize()) { container.select(Foo.class).get(); }
The container is also registered as a
Singleton
bean.Provides convenient access to beans, BeanManager and events, which is particularly helpful when bootstrapping an application in Java SE:
Foo foo = container.select(Foo.class).get(); container.getBeanManager().fireEvent(new Bar()) container.event().select(Bar.class).fire(new Bar());
- Author:
- Peter Royle, Martin Kouba
- See Also:
Weld
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.weld.inject.WeldInstance
WeldInstance.Handler<T>
-
-
Field Summary
-
Fields inherited from class org.jboss.weld.AbstractCDI
knownClassNames
-
Fields inherited from class jakarta.enterprise.inject.spi.CDI
configuredProvider, discoveredProviders
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
checkState()
Check whether the container is in a "valid" state, no-op by default.void
close()
Shuts down this SeContainer instance when it is no longer in scope.static WeldContainer
current()
A convenient method for {@link CDI.current()}.WeldEvent<Object>
event()
Provides access to all events within the application.BeanManager
getBeanManager()
Provides direct access to the BeanManager.String
getId()
Weld containers must have a unique identifier assigned when there are multiple Weld instances running at once.protected WeldInstance<Object>
getInstance()
Subclasses are allowed to override the default behavior, i.e.static List<String>
getRunningContainerIds()
Instance<Object>
instance()
Deprecated.Applications are encouraged to use methods for programmatic lookup directly.static WeldContainer
instance(String id)
boolean
isRunning()
Check if the container is running or was shut downvoid
shutdown()
Shutdown the container.-
Methods inherited from class org.jboss.weld.AbstractCDI
destroy, get, getCallingClassName, getHandler, getPriorityComparator, handlers, isAmbiguous, isResolvable, isUnsatisfied, iterator, select, select, select, select
-
Methods inherited from class jakarta.enterprise.inject.spi.CDI
setCDIProvider
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jakarta.enterprise.inject.Instance
destroy, isAmbiguous, isResolvable, isUnsatisfied, stream
-
Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Methods inherited from interface org.jboss.weld.inject.WeldInstance
handlersStream
-
-
-
-
Method Detail
-
instance
public static WeldContainer instance(String id)
- Parameters:
id
-- Returns:
- the running container with the specified identifier or
null
if no such container exists
-
current
public static WeldContainer current()
A convenient method for {@link CDI.current()}. Returns currentWeldContainer
instance if there is exactly one instance running. ThrowsIllegalStateException
in any other case.- Returns:
- Current
WeldContainer
instance if and only if exactly one instance exists
-
getRunningContainerIds
public static List<String> getRunningContainerIds()
- Returns:
- an immutable list of ids of running containers
-
instance
public Instance<Object> instance()
Deprecated.Applications are encouraged to use methods for programmatic lookup directly.Provides access to all beans within the application. Retained for backward compatibility - WeldContainer implements .- Returns:
- the instance
-
event
public WeldEvent<Object> event()
Provides access to all events within the application. For example:weldContainer.event().select(Bar.class).fire(new Bar());
- Returns:
- the event
-
getId
public String getId()
Weld containers must have a unique identifier assigned when there are multiple Weld instances running at once.- Specified by:
getId
in interfaceorg.jboss.weld.environment.ContainerInstance
- Returns:
- the container id
-
getBeanManager
public BeanManager getBeanManager()
Provides direct access to the BeanManager.- Specified by:
getBeanManager
in interfaceorg.jboss.weld.environment.ContainerInstance
- Specified by:
getBeanManager
in interfaceSeContainer
- Specified by:
getBeanManager
in classCDI<Object>
- Returns:
- the BeanManager
-
shutdown
public void shutdown()
Shutdown the container.- Specified by:
shutdown
in interfaceorg.jboss.weld.environment.ContainerInstance
- See Also:
Weld.initialize()
-
isRunning
public boolean isRunning()
Description copied from interface:SeContainer
Check if the container is running or was shut down- Specified by:
isRunning
in interfaceSeContainer
- Returns:
true
if the container was initialized completely and is not shut down yet,false
otherwise
-
close
public void close()
Description copied from interface:SeContainer
Shuts down this SeContainer instance when it is no longer in scope. Implemented from
AutoCloseable
,- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceSeContainer
-
getInstance
protected WeldInstance<Object> getInstance()
Description copied from class:AbstractCDI
Subclasses are allowed to override the default behavior, i.e. to cache an instance per BeanManager.- Overrides:
getInstance
in classAbstractCDI<Object>
- Returns:
- the
Instance
the relevant calls are delegated to
-
checkState
protected void checkState()
Description copied from class:AbstractCDI
Check whether the container is in a "valid" state, no-op by default.Subclasses are allowed to override the default behavior, i.e. to check whether a container is running.
- Overrides:
checkState
in classAbstractCDI<Object>
-
-