Open In App

Difference between EJB and Spring

Last Updated : 24 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • 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.

2. Spring :
Spring is an open source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework uses various techniques such as Aspect-Oriented Programming (AOP), Plain Old Java Object (POJO), and dependency injection (DI), to develop enterprise applications. This framework helps you to manages the business object. It made the development of Web applications much easier than compared to classic Java frameworks and Application Programming Interfaces (APIs), such as Java database connectivity (JDBC), JavaServer Pages (JSP), and Java Servlet. The features of the Spring framework such as Inversion of control (IoC), Aspect-Oriented Programming (AOP), and transaction management, make it unique among the list of frameworks.



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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads