Class WeldContainer

  • All Implemented Interfaces:
    AutoCloseable, Iterable<Object>, javax.enterprise.inject.Instance<Object>, javax.enterprise.inject.se.SeContainer, javax.inject.Provider<Object>, ContainerInstance, org.jboss.weld.inject.WeldInstance<Object>

    @Vetoed
    public class WeldContainer
    extends org.jboss.weld.AbstractCDI<Object>
    implements AutoCloseable, ContainerInstance, javax.enterprise.inject.se.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

        org.jboss.weld.inject.WeldInstance.Handler<T extends Object>
    • Field Summary

      • Fields inherited from class org.jboss.weld.AbstractCDI

        knownClassNames
      • Fields inherited from class javax.enterprise.inject.spi.CDI

        configuredProvider, discoveredProviders
    • 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 current WeldContainer instance if there is exactly one instance running. Throws IllegalStateException 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 javax.enterprise.inject.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 Instance.
        Returns:
        the instance
      • event

        public org.jboss.weld.events.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 interface ContainerInstance
        Returns:
        the container id
      • getBeanManager

        public javax.enterprise.inject.spi.BeanManager getBeanManager()
        Provides direct access to the BeanManager.
        Specified by:
        getBeanManager in interface ContainerInstance
        Specified by:
        getBeanManager in interface javax.enterprise.inject.se.SeContainer
        Specified by:
        getBeanManager in class javax.enterprise.inject.spi.CDI<Object>
      • isRunning

        public boolean isRunning()
        Specified by:
        isRunning in interface javax.enterprise.inject.se.SeContainer
        Returns:
        true if the container was initialized completely and is not shut down yet, false otherwise
      • close

        public void close()
        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface javax.enterprise.inject.se.SeContainer
      • getInstance

        protected org.jboss.weld.inject.WeldInstance<Object> getInstance()
        Overrides:
        getInstance in class org.jboss.weld.AbstractCDI<Object>
      • checkState

        protected void checkState()
        Overrides:
        checkState in class org.jboss.weld.AbstractCDI<Object>