Contexts and Dependency Injection (CDI) integration with EJB container

The CDI specification enhances the EJB component model with contextual life cycle management.

Relationship of the CDI to the EJB specification

The EJB specification defines a programming model for application components that access transactional resources in a multi-user environment. Concerns, such as role-based security, transaction demarcation, concurrency, and scalability are specified declaratively using annotations and XML deployment descriptors that are enforced by the EJB container at run time. EJB components might be stateful, but are not by nature, contextual.

The following session bean instances are obtained using dependency injection:
  • Contextual
  • Bound to a life cycle context
  • Available to other instances that launch in that context
  • Container creates an instance when needed
  • Container destroys an instance when context ends

The WebSphere® Application Server CDI container performs dependency injection on all session and message-driven bean instances, even instances that are not contextual instances. WebSphere Application Server CDI supports injection of CDI beans inside enterprise beans and vice versa.

Usage

Best practices: Use the following best practices when injecting enterprise beans:
  • Use the @Inject method for contextual injection of local session beans.
  • Use the @EJB method for remote session beans
See the following examples of using the @EJB method in CDI:

Define producers making the EJB available for non-contextual injection:

@Produces @EJB PaymentService paymentService;
Consume the injected types in other CDI beans:
@Inject PaymentService myPaymentService

Practical considerations

You can define CDI-style interceptors with interceptor bindings and decorators enterprise beans. Interceptors are declared using @Interceptors methods or in ejb-jar.xml files, which are called before interceptors and are declared using interceptor bindings. Interceptors are called before decorators.

WebSphere Application Server supports failover (activation and passivation) of CDI beans and enterprise beans, along with their interceptors and decorators. EJB failover support with CDI only works for stateful session beans and requires the same configuration as stateful session bean failover. See the stateful session bean failover for the EJB container topic for more information. Configure EJB failover with web HTTP session failover. See the Configuring for database session persistence and the Configuring memory-to-memory replication for the peer-to-peer mode (default memory-to-memory replication) topics for more information. Except for abstract decorators, failover services are based on currentWebSphere Application Server failover providers. Web session failover and EJB stateful session bean failover and configured separately.

When a contextual (@Injected) instance of an EJB container is destroyed as a result of going out of scope, and if the underlying EJB container was not already removed by direct invocation of a remove method by the application, the WebSphere Application Server CDI container removes the stateful session bean.
The WebSphere Application Server CDI container removes the stateful session bean when:
  • You use the @Inject method to create a contextual injection instance and that instance in an EJB container is destroyed as a result of going out of scope.
  • The underlying EJB container was not already removed by direct invocation of a remove method by the application.

You must also consider the scope and state propagation of CDI beans. The request and application scope CDI beans maintain state in their respective contexts across the web and EJB containers. For instance, a request-scoped CDI bean injected in a servlet holds its state when a business method on stateful session enterprise bean accesses the same request-scoped bean.