Difference between EJB and Spring
Last Updated :
24 Dec, 2020
Improve
EJB and Spring both are used to develop enterprise applications. But there are few differences exists between them. So, in this article we have tried to cover all these differences.
1. Enterprise Java Beans (EJB) :
EJB stand for Enterprise Java Beans. It is a server side software component that summarizes business logic of an application. An EJB container provides the run time environment for web related software elements including computer reliability, Java Servlet Lifecycle (JSL) management, transaction procedure and other web services. To run an EJB application, we need an application server such as Jboss, Glassfish, Weblogic, Websphere etc. It performs Life Cycle Management, Object Pooling, Transaction Processing, Security, etc. There are three types of EJB.
Difference between EJB and Spring :
- Session Bean - Session bean contains business logic that can be invoked by local, remote or webservice client. There are two types of session beans : (i). Stateful session bean and (ii). Stateless session bean.
- Message Driven Bean - Like Session Bean, it contains the business logic but it is invoked by passing message.
- Entity Bean - It summarizes the state that can be persisted in the database. It is deprecated. Now, it is replaced with JPA (Java Persistent API). There are two types of entity bean : (i). Bean Managed Persistence and (ii). Container Managed Persistence.
Difference between EJB and Spring :
Properties | EJB | Spring |
---|---|---|
Specification | EJB is a specification of Java EE. | Spring is a framework. |
Dependency Injection | It can inject anything in the container including EJB Data sources, JMS Resources, and JPA Resources. | It can inject anything including list, properties, map, and JNDI resources. |
Transaction Management | It supports only JTA transaction Manager. | It supports multiple transaction manager such as JTA, JDBC, and Hibernate. |
Persistence | It tightly integrated to JPA. | It supports various persistence technologies such as JDBC, Hibernate, JPA, and iBatis. |
Messaging | It provides out of the box messaging capabilities through Message Driven Beans. | We need to add listener configuration for each message. |
Scheduling | It provides simple scheduling through EJB Time Service. | We need to add and configure Quartz for scheduling. |
Security | It supports both declarative and programmatic security through JAAS. | It provides declarative security through the Spring configuration file. |
AOP | It provides limited support through interceptors. | It provides robust support through AspectJ. |