In the context of Jakarta EE 9, a CDI Managed Bean is a Java class managed by the Contexts and Dependency Injection (CDI) container, as defined by the CDI specification (JSR 365). CDI is a key component of Jakarta EE that provides a robust dependency injection and lifecycle management framework.
Definition and Characteristics
A CDI Managed Bean is a Java object that:
- Is instantiated and managed by the CDI container.
- Supports dependency injection, allowing other beans or resources (e.g., EJBs, JPA entities, or other CDI beans) to be injected into it using annotations like
@Inject
.
- Has a well-defined lifecycle and scope (e.g.,
@RequestScoped
, @SessionScoped
, @ApplicationScoped
, etc.), which determines how long the bean lives and how it is shared across different contexts.
- Can participate in CDI features like interceptors, decorators, and events for advanced functionality.
Key Features
- Automatic Discovery: By default, CDI scans classes in a Jakarta EE application for managed beans. A class qualifies as a CDI managed bean if:
- It is not an EJB (though EJBs can also be CDI managed beans in certain cases).
- It is not an inner class or abstract class (unless explicitly annotated).
- It has a constructor with no parameters or a constructor annotated with
@Inject
.
- It is in a bean archive (explicit or implicit).
- Bean Archives:
- CDI beans are discovered in bean archives, which are JARs or WARs containing a
beans.xml
file (even if empty) in the META-INF
or WEB-INF
directory.
- In Jakarta EE 9, the default scanning mode is
annotated
, meaning only classes with specific CDI annotations (e.g., @Named
, @RequestScoped
, etc.) are considered managed beans unless overridden in beans.xml
.
- Annotations:
- Dependency Injection: The
@Inject
annotation is used to inject dependencies into the bean.
- Scopes: Annotations like
@RequestScoped
, @SessionScoped
, @ApplicationScoped
, or @Dependent
define the lifecycle and visibility of the bean.
- Qualifiers: Annotations like
@Named
or custom qualifiers help resolve ambiguities when multiple beans of the same type are available.
- Interceptors and Decorators: CDI managed beans can use
@Interceptor
or @Decorator
for cross-cutting concerns like logging or transaction management.
- Lifecycle Management:
- The CDI container manages the creation, injection, and destruction of beans.
- Beans can have lifecycle callbacks like
@PostConstruct
(called after creation) and @PreDestroy
(called before destruction).
Example
import jakarta.inject.Inject;
import jakarta.enterprise.context.RequestScoped;
import jakarta.annotation.PostConstruct;
@RequestScoped
public class UserService {
@Inject
private UserRepository userRepository;
@PostConstruct
public void init() {
System.out.println("UserService initialized");
}
public String getUserName() {
return userRepository.findUserName();
}
}
In this example:
UserService
is a CDI managed bean with @RequestScoped
, meaning a new instance is created for each HTTP request.
- The
@Inject
annotation injects a UserRepository
dependency.
- The
@PostConstruct
method is called after the bean is instantiated and dependencies are injected.
Requirements for a CDI Managed Bean
To qualify as a CDI managed bean in Jakarta EE 9, a class must:
- Be a concrete class or annotated with @Decorator or @Interceptor.
- Not be annotated with @Vetoed or belong to a package/class explicitly excluded in beans.xml.
- Be in a bean archive (implicit or explicit).
- Have a no-arg constructor or one annotated with @Inject.
Jakarta EE 9 Context
In Jakarta EE 9, CDI 2.0 (or later) is used, which is part of the Jakarta CDI specification. Key changes in Jakarta EE 9 include:
- The shift from
javax.*
to jakarta.*
namespace (e.g., jakarta.inject.Inject
instead of javax.inject.Inject
).
- Enhanced modularity and compatibility with modern Java SE versions.
- Continued support for features like dependency injection, event handling, and interceptors, with improved integration with other Jakarta EE components like Jakarta REST (JAX-RS) and Jakarta Persistence (JPA).
Summary:
A CDI Managed Bean in Jakarta EE 9 is a Java class managed by the CDI container, enabling dependency injection, lifecycle management, and advanced features like interceptors and events. It is defined by its annotations, scope, and inclusion in a bean archive, making it a core building block for modular, loosely coupled applications in Jakarta EE.