EJB vs. Spring
EJB and Spring both are technologies that are capable to provide middleware solutions. But it's the development philosophy are different. Both provide similar a functional range but the code looks identical. In this section, we will discuss the difference between EJB and Spring.
EJB is an acronym for Enterprise JavaBeans. It is a Java API that is used for distributed and component-based programming. It specifies the bean format. It outlines a set of services provided by the container on which the bean runs. For this reason, developers need not to worried about services like security, transaction support, etc. An application server (such as JBoss, WebLogic, etc.) is required to run an EJB application. Although, the Spring framework can be sued with any application server. Spring applications are locked into both Spring itself and the specific services you choose to integrate into the Spring framework.
Spring is an open-source, lightweight framework. It offers many features such as IOC, transaction management, JDBC exception handling, etc. It has a layered architecture that allows us to choose the necessary components while starting development. It also supports other frameworks such as Hibernate, JSF, EJB, etc. Spring framework forces the developers to emphasize the business requirements rather than the architectural details of the application.
The design of Spring Framework is such that it sits on the top of the application servers and service libraries. While in EJB 3.0, the framework is integrated into the application servers and the service integration code is encapsulated behind an interface. Thus, it can optimize the performance and developer experience by working at the application server level. For example, they can tie the JPA engine closely to JTA transaction management. Therefore, EJB is not perfect because some features are still missing like an injection of non-managed components like simple POJOs.
Difference Between EJB and Spring
The key difference between EJB and Spring is that EJB is a specification of Java EE while Spring is a framework or an implementation. Another key difference is that Spring does not support propagation of transaction context across remote calls while EJB does the same.
EJB has been specially designed for transactional and component programming. It is used to develop the server-side application. While Spring provides the infrastructure configuration and lets the developer focus on business requirements.
What to use?
If we want to develop such an application that has lots of configuration and that has control at the container level, we should go with Spring. If we do not want to spend too much time in configuration and the application has a pre-defined tech stack, we can use EJB.
Can Spring replace EJB?
Spring was developed as an alternative to EJB from its beginning. All the components of EJB are integrated into Spring. Hence, we can use Spring instead of EJB. Spring encourages good object-oriented design practice. The advantage of using Spring is that we can easily switch to other DI frameworks such as Guice.