JavaData & JavaWhat Are Enterprise Java Beans (EJB)?

What Are Enterprise Java Beans (EJB)? content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

To put it simply, Enterprise Java Beans (EJB) is a Java Bean that works in an Enterprise Environment. And, a Java Bean is a POJO designed according to the norms of the Java Bean Specification. An EJB class is defined by Java Specification Request (JSR) 345 to work in an enterprise framework. Although there are a lot of intricacies involved with EJB implementaion in an enterprise arena, the basic idea is simple. Because EJB is associated with the paradigm of enterprise development, it is quite necessary to understand the intricacies behind the Java Enterprise Environment (JEE) and the role EJB plays in it. This article shall try to delineate the concept behind JEE with a focus on EJB in particular.

EJB Overview

The release of EJB is managed by the Java Community Process (JCP) as a Java Specification Request (JSR). The recent release version EJB 3.2 is defined by JSR 345. Prior to EJB 3.0, persistent components were a part of the EJB model. Later, JPA emerged as a separate component, managed by its own JSR. Nonetheless, they complement each other and often serve together in dealing with enterprise application development. The component model of EJB comprise three object types, such as:

  • Session beans, which may be stateful, stateless, or singleton. Session beans specifically perform business service tasks for the client. They are generally configured to operate in the context of distributed transaction and access-control. Session beans also work as Web service endpoints such as a reference between client and server endpoints.
  • Message-driven beans are used in association with a service providing an asynchronous response to external events. The asynchronous endpoints consume message objects from the message queue.
  • As stated earlier, The entity beans as designated as JPA have been usurped but provide persistence service, complementing EJB’s component model architecture. Entity beans are objects that represent persistent business data. Because entity beans are separately managed by the persistence provider and not by the EJB container, they are not strictly considered as enterprise beans.

EJB in a Java Enterprise Environment (JEE)

JEE can be viewed nominally as a three-tier architecture: Web container for life-cycle management of Java Servlets, JSP and managed beans; EJB container for life-cycle management of Enterprise Java Beans; and Persistence layer for providing JPA service to the enterprise system.

Figure 1: The JEE environment

Since EJB3, we can use declarative metadata to define the behavior of EJB for both enterprise beans and entity beans. Declarative metadata means using XML descriptors or Java Annotation to define the behavior of the EJB classes. Thus, the choice is to use Java Annotation or XML descriptors, or both can be used simultaneously. This ability provides ease of customization in the long run, because at a later point developers do not have to encumber the Java source with service implementation code. A simple change in the declarative metadata may suffice the much needed change. However, in case of conflicting metadata declaration between Java Annotation and XML descriptors, especially when both are used, XML descriptors take precedence.

EJB Container

An EJB container provides the supporting environment for enterprise beans. The services provided by the environment are such as component life-cycle service, security service, transactional service, pooling and caching of resources, concurrency, and so forth. The business logic encoded in EJBs defines the type of service it wants to use. The encoded logic uses EJB-specific metadata that is interpreted by the container and defines the behavior of the EJB at run time or during deployment. Some of the core service provided by the container are as follows:

  • Dependency Injection: Sometimes, we need the EJB to be instantiated prior to providing service to the client. The EJB container implicitly creates the object and initializes property data of the EJB according to the rules defined for that particular enterprise bean. This feature is called dependency injection. Dependency injection is commonly used in injecting EntityManager and UserTransaction into session bean. EntityManager is used with reference to entities interaction with persistent unit, and UserTransaction is used with reference to managing transaction demarcation.
  • State Management: The state of the stateful session beans is managed separately. As a result, they are distinct for each client.
  • Pooling: The container manages a pool of stateful session bean instances that can be shared among multiple clients.
  • Component Life Cycle: The container manages the life cycle of each EJB component.
  • Remote Client Communication: An EJB component declared as remote beans can invoke a method remotely via standard protocols such as using RMI-IIOP over a network connection.
  • Transaction Management: An EJB component declares its participation in a transaction with the help of annotation. This informs the container about the transaction policy and takes care of the commit and rollback procedure accordingly.
  • Interceptors: Cross-cutting concerns are defined by callback methods, called interceptors. These methods are invoked automatically by the container when a certain life-cycle events occurs.
  • Security: The container manages user and role authorization of class and method level access defined by EJB.
  • Concurrency Support: EJBs are thread safe except for singletons where concurrency declaration is required. The container manages the concurrency issues, relieving concern for programmers.

A Quick Example

EJB defines the business logic for an enterprise application where the heavy duty of pooling, multithreading, and security are handled by the container. The following code snippet shows how simply we can create a session bean with annotation.

package org.mano.entity;

import javax.persistence.*;

public class Person implements Serializable {
   @GeneratedValue(strategy = GenerationType.AUTO)
   private int id;
   private String name;

   public Person() {

   public Person(int id, String name) { = id; = name;

   public int getId() {
      return id;

   public void setId(int id) { = id;

   public String getName() {
      return name;

   public void setName(String name) { = name;

package org.mano.entity;
import javax.ejb.Stateless;
import javax.persistence.*

public class PersonBean {

   @PersistenceContext(unitName = "WebApplication2PU")
   private EntityManager entityManager;

   public Person getPerson(int id) {
      return entityManager.find(Person.class, id);

   public void add(Person p) {

   public void remove(Person p) {

   public void update(Person p) {



The spectrum of EJB is more extensive than the rudimentary explanation given in this article. Interested readers may refer to the references given below for detailed information. This article picked up only the key ideas to begin with what EJB actually is. In contrast to its prior releases, EJB 3 has become simpler to implement and more flexible to use. Try creating a EJB fundamental class in a Web application project (use NetBeans + Glassfish to begin with) and see for yourself how the mechanism actually works. Take the help of any Java EE book/documentation/tutorial of your choice along with invaluable EE API documentation (shipped with Netbeans). The code given here is part implementation; other classes and HTML/JSF files are needed to execute this code in an Application Server.


  • Antonio Goncalves, Beginning Java EE7. Apress
  • J. Weatherbee, C Rathod, R. Kodali, P. Zadrozny. Beginning EJB 3. Apress
  • Java EE7 API Documantation

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories