EJB 1 vs EJB 2 vs EJB 3
Ejb1 had limited functionality just as a lady bug that doesn’t do much work still is appreciated by people due to its beauty.
Ejb2 was very heavy like the Elephant with functionalities. Still to take proper use of the elephant power it had to be tamed, or Ejb2 was later simplified to Ejb3
Ejb3 is very simple having power and is serving the people very well like a cow giving milk to the universe.
Overview of EJB
Ejb is a platform for building ,reusable, portable and scalable business applications using java.
Ejb helps developers to concentrate on building business logic code without having to build infrastructure code comprising of functionalities including transactions, security, automated persistence that are provided by the Ejb container. Ejb is a specification which is followed by a large group of user vendors creating application server. So all have to follow the same specification making our applications portable.
EJB as Component
Ejbs are server side components that you can use to build parts of an application like business logic or persistence code. These components in Ejb3 are simple POJO with special power. Component means that the user need not know the inner working of the Component, he only needs to know what he has to give and what the Component will give you i.e. application behavior has to be completely encapsulated from the outside world.
Types of EJB
There are 3 types of Ejbs. Session beans taking care of business logic. Entity Bean used for persistence. Message driven beans used for processing asynchronous messages.
Use of EJB
One of the most important use of Ejb is its reusability. E.g. If there is a site for online books that is using an Ejb code for checking credit card. Then if we create another Website for CDs then we can reuse the same credit card Ejb. The first Ejb can be put as an Ejb component or a web service that is used by both applications.
The Ejb Container houses the Ejb components we develop. This Ejb Container together with the Ejb Component act as a Framework with predefined functionalities. This relieves the programmer from coding certain areas like transaction, security and persistence that are provide by the Ejb Container itself. Like if a programmer is creating a bean related to processing a credit card he need not create coding for the Transaction and Security that is provided by the Ejb Container Automatically.
We can use metadata annotations in our beans to specify what all services and functionalities we require. These are added dynamically when required during deployment. Metadata annotations is a way to specify the properties in a POJO, and these functionalities get automatically added. So we can say a POJO + Metadata annotation=Ejb3 bean. So we don’t need any outside xml configuration files.
Domain Driven Design
DDD is the preferred way as compared to the traditional four tired architecture followed by Ejb 2.
Following DDD the domain objects should contain buisness logic and shouldn’t be just a replica of database record. Incase of Ejb 2 beans were not POJOS and didn’t support OO features like inheritance, polymorphism.
Ejb3 helps follow DDD. Beans created using Ejb3 JPA supports polymorphism, inheritance etc.
Comparison of EJB 1,EJB 2 and EJB 3
|Aspect||EJB 1||EJB 2||EJB 3|
|Release Year||Late 1990s||Early 2000s||Around 2006|
|Key Features||Automatic Persistence||Entity Beans||Annotations, POJO model|
|Remote Method Invocation.||Session Beans||Java Persistence API (JPA)|
|Transaction Support||Message-Driven Beans||Dependency Injection (DI)|
|Programming Model||Interfaces||Extends EJB classes||POJO (Plain Old Java Object)|
|Persistence||Basic||CMP, BMP||Java Persistence API (JPA)|
|Resource Injection||N/A||N/A||Dependency Injection (DI)|
Why choose EJB 3 among other technologies
Ease of use, Integrated solution stack— Ejb3 offers a complete stack of server solutions, including persistence, dependency injection, interceptors i.e. we won’t need to look for third party solutions to integrate in our application.