Contexts and Dependency Injection (CDI)
Learn about Contexts and Dependency Injection 1.2 for the Java EE platform (CDI 1.2), which is a JSR 346 implementation based on JBoss Weld.
CDI is activated in an application either by the presence of a beans.xml file inside that module, or by bean defining annotations defined in the JSR 346 specification. We can find the beans.xml file in the WEB-INF directory of a web archive (WAR), or META-INF directory of other types of archives. When activated, the container provides services such as:
- Context management
- Type-safe dependency injection: A CDI-managed bean is instantiated and injected as needed.
- Decorators, which implement one or more bean interfaces and can contain business logic. Decorators are disabled by default. We can have multiple decorators per bean and order is defined by the beans.xml file. Use beans.xml or @Priority to enable the decorators.
- Interceptor bindings: Interceptors, which are enabled manually in the beans.xml file or enabled globally with @Priority, are bound using an interceptor binding type.
- Event model
- Integration into JavaServer Faces (JSF) and JavaServer Pages (JSP) files that use the Expression Language (EL)
- JavaEE component classes that support injections and the use of interceptors
The specification-related API classes for JSR 299 and JSR 330 and IBM modified implementation classes that are based on Apache OpenWebBeans are packaged with the application server runtime environment.
Although the product CDI implementation is based on Apache OpenWebBeans, the following changes are added to OpenWebBeans to support integration with the server run time:
- Integration with other Java EE containers in WebSphere Application Server traditional that support injectable components.
- ScannerService implementation that uses the WAS traditional byte code scanner.
- Direct use of product EJB metadata for determining EJB types.
- Automatic registration of Servlet Listeners, Filters, and Interceptors for CDI applications that no longer need to be added by each application.
- Product-specific implementations of many OpenWebBeans Service Programming Interfaces (SPI), such as ResourceInjectionService, TransactionService, and failover service.
Important: Container-managed transactions and security are not provided by CDI.
CDI is only supported by the default WAS traditional class loader policy, Class loader for each WAR file in application, and not with the alternative, single class loader for application setting.
Subtopics
- Contexts and Dependency Injection 1.2 behavior changes
The Contexts and Dependency Injection (CDI) 1.2 implementation contains some behavior changes that might cause an application that was migrated from CDI 1.0 to behave differently or fail on CDI 1.2.- CDI integration with JavaServer Faces
Contexts and Dependency Injection (CDI) primarily integrates with JavaServer Faces (JSF) through the Expression Language (EL) and enables CDI beans to be exposed through the unified EL-to-JSF components. CDI also provides a built-in context for conversation scope that is active during standard JSF life cycle phases.- Contexts and Dependency Injection (CDI) integration with EJB container
The CDI specification enhances the EJB component model with contextual life cycle management.
Troubleshoot contexts and dependency injection Developing applications that use Contexts and Dependency Injection (CDI) Contexts and Dependency Injection (CDI)