Enterprise Java Beans (EJB)
Enterprise Java Beans (EJBs) is an API within the Java EE specification that provides a standard component architecture for building object-oriented business applications.
EJBs are reusable Java objects or POJOs that run in a EJB container that handles application logic to support the business operations of a system. By isolating system or business logic in a container, it can be reused by multiple clients as well as benefit from container services such as transactions, connection pooling or multi-threading without having to write explicit code.
Earlier versions of EJB were known to be quite complex and required a considerable amount of explicit Java code and XML to be implemented. Newer versions introduced annotations for simpler configuration, usage and deployment.
EJB Architecture

When designing a system architecture for EJBs, interfaces are defined for the EJB and an implementation is developed. The EJBs will handle the core logic for data persistence for example. The EJB implementation will run inside of the EJB container and is injected into other Java components such as JSF backing beans or servlets through a JNDI lookup of the EJB.
Once the EJB is injected into these components. they can leverage the business services contained within the EJB and benefit from the supporting container services. This architecture will pull core business logic out of web tier components so that they can be centralized and reused.
Benefits
Supporting services provided by the EJB container include :
- Transactions
- Security
- Concurrency
- Networking
- Persistence
EJB Types
Session Beans
Session Beans contains reusable application logic that models an action or use case. There are 3 types of session beans :
| Type | Description | Annotation |
| Stateful | Unique for every client and is capable of maintaining the conversational state with the client or use case. | @Stateful |
| Stateless | Shared for every client and does not contain any state | @Stateless |
| Singleton | Shared for every client and capable of maintaining state at an application level | @Singleton |
Message Driven Bean
Message driven beans are stateless components that run on the server to process messages sent by other components such as Java Messaging Service (JMS).
