Context and Dependency Injection (CDI)

Context and Dependency Injection (CDI) provides a common mechanism to inject component such as Enterprise JavaBeans (EJBs) or managed beans into other components such as JavaServer Pages (JSPs) or other EJBs.

CDI support is provided by Liberty, and is configured in the Liberty server configuration files (server.xml and included files) through the features that are described in Table 1.

Table 1. Liberty features that provide CDI support
Feature Name CDI Specification Level Specification Levels
cdi-1.0 CDI 1.0
Note: CDI 1.0 support in Liberty is stabilized. For more information, see Stabilized Liberty features and feature capabilities.
  • Java™ EE 6.0 - Web Profile
cdi-1.2 CDI 1.2
  • Java EE 7.0 - Web Profile
  • Java EE 7.0 - Full Profile
  • Eclipse MicroProfile 1.0
  • Eclipse MicroProfile 1.2
cdi-2.0 CDI 2.0
  • Java EE 8.0 - Web Profile
  • Java EE 8.0 - Full Profile
  • Eclipse MicroProfile 1.2
  • Eclipse MicroProfile 3.0
  • Eclipse MicroProfile 4.1
cdi-3.0 CDI 3.0
  • Jakarta EE Web Profile 9.1
  • MicroProfile 5

CDI beans can be packaged in any of the following archive types: JARs, EJB JARs, or WARs. Any location on the application class path can contain CDI beans. For CDI beans to be registered into the container, the archive they are contained in must be declared as a bean archive. In CDI 1.0, only explicit bean archives are supported. CDI 1.1 introduces support for two types of bean archives, explicit bean archives and implicit bean archives.

Explicit bean archives must contain a beans.xml file, which must be at:
  • META-INF/beans.xml for JAR types
  • WEB-INF/beans.xml, or WEB-INF/classes/META-INF/beans.xml for WAR types.
The beans.xml file can be empty, or can contain XML data that is described by either the CDI 1.0 or CDI 1.1 schemas.
Note: CDI versions 1.2 and 2.0 use the version 1.1 schema.

Implicit bean archives must contain one or more bean classes with a bean defining annotation, or session bean.

Liberty provides a configuration option on whether implicit bean archives are enabled. In the CICS® default server.xml template file, implicit bean archives are disabled. If implicit bean archives are enabled in Liberty, every application is scanned for the presence of CDI beans, causing unnecessary CPU usage. On a platform where CPU is chargeable, a more efficient approach is to disable implicit bean archives, and define applications that contain CDI beans as explicit bean archives by adding a beans.xml file.

Implicit bean archives can be enabled through the following configuration:

<cdi12 enableImplicitBeanArchives="true" />

Methods within CDI beans can be defined as entry points to CICS programs. For more information, see Linking to Java applications in a Liberty JVM server by using the @CICSProgram annotation.