Bootstrapping a CDI Container in Java SE

This chapter explains how to use the API for bootstrapping a CDI container in Java SE. This capability allows you to run CDI applications on Java SE and obtain beans, independently of an application server or any Jakarta EE APIs.

For more information about bootstrapping a CDI container in Java SE, see the Weld Reference Guide at https://weld.cdi-spec.org/documentation/.

The Bootstrap API

The API for bootstrapping a CDI container in Java SE consists of the following entities:

  • jakarta.enterprise.inject.se.SeContainerInitializer class – Allows you to configure and bootstrap the CDI container. This class includes the following key methods:

    • newInstance() obtains a SeContainerInitializer instance, which allows you to configure the container prior to bootstrapping it.

    • initialize() bootstraps the container.

  • jakarta.enterprise.inject.se.SeContainer interface – Provides access to the BeanManager instance for programmatic lookup, as defined in the SeContainer interface, which is described at https://jakarta.ee/specifications/cdi/3.0/jakarta-cdi-spec-3.0.html#se_container.

Configuring the CDI Container

The configuration of the SeContainerInitializer instance allows the explicit addition of elements into an internal synthetic bean archive. The synthetic bean archive represents the set of beans that have been loaded while initializing the container. The contents of the synthetic bean archive depend on whether discovery is enabled:

  • If discovery is enabled, the synthetic bean archive is created using standard bean discovery rules and contains a superset of all JAR files on the classpath. Archives that do not include a beans.xml file are excluded.

  • If discovery is disabled, and beans are added programmatically, the synthetic bean archive contains only the beans that have been programmatically added.