| Lesson 2 | Jakarta Enterprise Beans versus CDI Managed Beans |
| Objective | Identify differences between Jakarta Enterprise Beans and CDI Managed Beans |
Java has used the word bean in several different ways, and that has caused confusion for generations of Java students. A JavaBean, an Enterprise JavaBean, a Jakarta Enterprise Bean, a CDI managed bean, a JSF backing bean, and a Spring bean are not the same thing. They share a naming convention, but they belong to different programming models and different historical periods of Java development.
The original version of this lesson compared Enterprise JavaBeans with JavaBeans. That comparison made sense in older J2EE courses because students needed to distinguish Java SE-style JavaBeans from server-side Enterprise JavaBeans. In a Jakarta EE 11 course, however, the more useful comparison is between Jakarta Enterprise Beans and CDI managed beans. JavaBeans should still be understood as historical and conventional Java objects, but they are not the modern Jakarta EE managed component model.
This lesson explains the difference among three related terms: JavaBeans, Jakarta Enterprise Beans, and CDI managed beans. The goal is to help you recognize which kind of “bean” is being discussed, what container or convention manages it, and which programming model is appropriate for modern Jakarta EE development.
The word bean originally became popular in Java because JavaBeans were reusable software components. A JavaBean followed naming and property conventions that allowed tools and frameworks to discover its properties. Later, Enterprise JavaBeans used the same word for a very different kind of server-side enterprise component. Still later, CDI made managed beans central to Jakarta EE application development.
Because the terminology overlaps, a developer must always ask a precise question: what kind of bean is this? A JavaBean is usually a plain Java object that follows property conventions. A CDI managed bean is an object managed by the CDI container. A Jakarta Enterprise Bean is a business component managed by the Enterprise Beans container and supported by Jakarta EE enterprise services.
The similarity in names should not obscure the technical differences. JavaBeans do not automatically provide transactions, container-managed security, remote interfaces, dependency injection, pooling, timers, or enterprise lifecycle services. CDI managed beans and Jakarta Enterprise Beans are container-managed components, but they are used for different purposes.
A JavaBean is a Java class that follows a set of conventions. Historically, JavaBeans were associated with Java SE, visual builder tools, property editors, introspection, and reusable client-side components. A JavaBean was not automatically an enterprise server component. It was a Java object designed to be recognized and manipulated by tools or frameworks.
A traditional JavaBean commonly has a public no-argument constructor, private fields, getter and setter methods, and predictable property names. For example, a field named firstName would normally be accessed through getFirstName() and setFirstName(). This convention allowed tools and frameworks to discover properties without requiring custom metadata for every class.
public class CustomerBean {
private String firstName;
private String lastName;
public CustomerBean() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
This example is a JavaBean-style class because it follows property conventions. However, nothing in this class makes it a Jakarta EE component. It is not automatically injected by CDI, it does not automatically participate in transactions, and it is not automatically exposed as a remote business service.
JavaBean conventions are still relevant because many frameworks use property names, getters, setters, and plain Java objects. However, JavaBeans should not be confused with CDI managed beans or Jakarta Enterprise Beans. A JavaBean is primarily a convention. A CDI managed bean or Jakarta Enterprise Bean is managed by a container.
Enterprise JavaBeans, usually abbreviated EJB, were created for server-side enterprise Java applications. In older J2EE and Java EE systems, EJBs provided a standard way to build business components that could run inside an application server. The EJB container managed infrastructure services so developers could focus on business logic.
Historically, Enterprise JavaBeans were associated with distributed objects, remote interfaces, home interfaces, deployment descriptors, transaction attributes, security roles, pooling, and server-side component deployment. An EJB could represent business logic that was accessed locally within the same application or remotely from another JVM or client program.
The original lesson correctly recognized that Enterprise JavaBeans and JavaBeans were not the same thing. However, older descriptions often overstated the idea that all Enterprise JavaBeans were remote distributed objects. Remote EJBs were important historically, but not every enterprise bean was remote, and modern Jakarta EE applications usually avoid fine-grained remote-object design.
The key historical lesson is this: Enterprise JavaBeans were container-managed business components, while JavaBeans were Java SE-style property-convention objects. They shared the word “bean,” but they belonged to very different programming models.
The modern Jakarta name for Enterprise JavaBeans is Jakarta Enterprise Beans. Jakarta Enterprise Beans remains a component-based business application technology in the Jakarta EE ecosystem. It is still useful when an application needs specific Enterprise Beans services, but it is no longer the default mental model for every business object in a Jakarta EE application.
Jakarta Enterprise Beans may be appropriate when an application needs declarative transaction attributes, message-driven beans, container-managed timers, stateful conversational components, singleton beans with container-managed concurrency, or compatibility with an existing EJB architecture. These features can still be valuable in enterprise systems that are already built around Enterprise Beans or that need the services supplied by an Enterprise Beans runtime.
The main Enterprise Beans categories students should understand are stateless session beans, stateful session beans, singleton session beans, and message-driven beans. Stateless session beans support reusable business operations without storing client-specific conversational state. Stateful session beans maintain client-specific state across calls. Singleton session beans provide one shared component instance. Message-driven beans respond to asynchronous messages.
Entity beans should be treated as legacy. Older EJB courses often taught entity beans as part of the enterprise bean family, but modern Jakarta EE uses Jakarta Persistence entities for object-relational persistence. Students should not learn entity beans as a recommended modern persistence model.
CDI stands for Contexts and Dependency Injection. CDI managed beans are the modern default component model for many Jakarta EE applications. A CDI managed bean is a class whose lifecycle, dependencies, and contextual behavior are managed by the CDI container.
CDI provides type-safe dependency injection, scopes, qualifiers, producers, events, interceptors, decorators, and integration with other Jakarta EE technologies. A CDI bean can be application-scoped, request-scoped, session-scoped, dependent-scoped, or placed in another supported scope. CDI allows one component to ask for
another component using @Inject, which avoids manual object construction and makes the application easier to assemble and test.
import jakarta.enterprise.context.ApplicationScoped;
@ApplicationScoped
public class CustomerService {
public String formatCustomerName(String firstName, String lastName) {
return lastName + ", " + firstName;
}
}
This class is not an Enterprise Bean. It is a CDI managed bean because CDI manages it in the application scope. It can be injected into other CDI beans, REST resources, persistence services, or controllers.
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
@Path("/customers")
public class CustomerResource {
@Inject
private CustomerService customerService;
@GET
public String example() {
return customerService.formatCustomerName("Ada", "Lovelace");
}
}
This example shows a common modern Jakarta EE pattern. A Jakarta REST resource receives the HTTP request, and CDI injects an application service that performs business work. The remote boundary is the REST endpoint, not a fine-grained remote object reference.
| Feature | JavaBean | CDI Managed Bean | Jakarta Enterprise Bean |
|---|---|---|---|
| Main context | Java SE property convention and historical builder tools | Jakarta EE application components | Jakarta EE enterprise business components |
| Managed by a Jakarta EE container | No, not by itself | Yes, by the CDI container | Yes, by the Enterprise Beans container |
| Dependency injection | No standard JavaBean dependency injection | Yes, dependency injection is central | Yes, integrated with Jakarta EE services |
| Transactions | No built-in transaction service | Available through Jakarta EE transaction integration | Strong declarative transaction model |
| Security | No built-in enterprise security | Available through Jakarta EE integration | Container-managed security support |
| Remote invocation | No | Not normally remote by itself | Can support local or remote enterprise use cases |
| Lifecycle and scopes | Plain object lifecycle | CDI scopes such as request, session, application, and dependent | Enterprise bean lifecycle, pooling, timers, and container services |
| Main modern use | Data carrier, DTO, or property-convention object | Default Jakarta EE component model for ordinary application services | Specialized enterprise services when Enterprise Beans features are needed |
| Persistence role | May be used as a DTO or POJO | May coordinate persistence services | Entity beans are legacy; use Jakarta Persistence instead |
| Recommended for new Jakarta EE business services | Not as the managed component model | Usually yes | Only when Enterprise Beans features are needed |
CDI managed beans are usually the best starting point for ordinary Jakarta EE application components. They are appropriate for business services, application services, REST resource collaborators, validation helpers, persistence coordination, event producers, event observers, and request-scoped or application-scoped logic.
Common CDI annotations include @Inject, @ApplicationScoped, @RequestScoped, @SessionScoped,
@Dependent, @Produces, @Observes, qualifiers, and interceptor bindings. These annotations allow Jakarta EE applications
to be assembled declaratively without turning every component into an Enterprise Bean.
In a modern Jakarta EE application, a CDI managed bean often contains business logic that would have been placed in an EJB in older systems. If the application does not need an Enterprise Beans feature, CDI is usually simpler and more natural.
Jakarta Enterprise Beans should be considered when the application needs Enterprise Beans services. These may include declarative EJB transaction attributes, container-managed timers, message-driven beans, stateful conversational components, singleton beans with container-managed concurrency, or compatibility with an existing EJB-based application.
Enterprise Beans are also relevant when the target runtime supports Enterprise Beans Lite or the full Enterprise Beans specification and the application has a clear reason to use that model. The important point is that Enterprise Beans should be selected because their services are needed, not simply because a class contains business logic.
For most ordinary application services, CDI managed beans are the modern default. For specialized enterprise services, Jakarta Enterprise Beans may still be appropriate.
The term “bean” is overloaded across the Java ecosystem. A JavaBean may mean a plain object that follows getter and setter conventions. A CDI bean means an object managed by CDI. A Jakarta Enterprise Bean means a server-side business component managed by the Enterprise Beans container. A JSF backing bean is a component used by a Jakarta Faces view. A Spring bean is an object managed by the Spring container.
Because the term is overloaded, students should never rely on the word “bean” alone. Always identify the framework, container, or convention being discussed. A JavaBean, CDI managed bean, and Jakarta Enterprise Bean may all be Java classes, but they are not managed in the same way and do not provide the same services.
The original objective was to identify differences between Enterprise JavaBeans and JavaBeans. That remains useful historically, but the modern objective is broader:
Students should be able to distinguish JavaBeans, CDI managed beans, and Jakarta Enterprise Beans, and explain when each term is appropriate in a Jakarta EE 11 application.
After completing this lesson, students should be able to:
Enterprise JavaBeans and JavaBeans share part of a name, but they are not the same technology. JavaBeans are Java SE-style property-convention objects. Enterprise JavaBeans were server-side enterprise components from the J2EE and Java EE era. In Jakarta EE, the modern name is Jakarta Enterprise Beans.
For Jakarta EE 11, the more important comparison is Jakarta Enterprise Beans versus CDI managed beans. CDI managed beans are the modern default for many application services because CDI provides dependency injection, scopes, lifecycle management, qualifiers, producers, events, and interceptors. Jakarta Enterprise Beans remain useful when an application specifically needs Enterprise Beans services such as timers, message-driven beans, stateful session behavior, singleton concurrency, or EJB-style transaction attributes.
The practical rule is simple: use the word “bean” carefully. Identify whether the class is a JavaBean by convention, a CDI managed bean by container management, or a Jakarta Enterprise Bean by enterprise component services. In modern Jakarta EE development, CDI managed beans are usually the first choice, while Jakarta Enterprise Beans are used when their specialized container services are required.