95

Since Spring is able to use transactions just like EJB. For me, Spring is able to replace the requirement of using EJB. Can anyone tell me what are the extra advantages of using EJB?

sal
  • 23,373
  • 15
  • 66
  • 85
cometta
  • 35,071
  • 77
  • 215
  • 324

4 Answers4

202

Spring was developed as an alternative to EJB right from its inception, so the answer is of course you can use Spring in place of EJBs.

If there's an "advantage" to using EJBs, I'd say that it would depend on the skills of your team. If you have no Spring expertise, and lots of EJB experience, then maybe sticking with EJB 3.0 is a good move.

App servers written to support the EJB standard can, in theory, be ported from one compliant Java EE app server to another. But that means staying away from any and all vendor-specific extensions that lock you in to one vendor.

Spring ports easily between app servers (e.g., WebLogic, Tomcat, JBOSS, etc.) because it doesn't depend on them.

However, you are locked into Spring.

Spring encourages good OO design practices (e.g., interfaces, layers, separation of concerns) that benefit any problem they touch, even if you decide to switch to Guice or another DI framework.

Update: This question and answer are five years old in 2014. It needs to be said that the world of programming and application development have changed a great deal in that time.

It's no longer just a choice between Java or C#, Spring or EJBs. With vert.x it's possible to eschew Java EE altogether. You can write highly scalable, polyglot applications without an app server.

Update: It's Mar 2016 now. Spring Boot offers an even better way to write applications without Java EE app servers. You can create an executable JAR and run it on a JVM.

I wonder if Oracle will continue to support the Java EE spec. Web services have taken over for EJBs. The EJB solution is dead. (Just my opinion.)

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 7
    In my opinion, "lock in" is too strong a phrase to describe Spring. After all, Spring's designed to glue everything together, not to replace them, you can always choose what you want to integrate with. Besides, everything has lock-ins, even the simplest Apache Commons lock us in, but we're still using it everyday. – Christopher Yang Jun 06 '13 at 13:52
  • 3
    There are no alternative vendors to VMWare/Spring the way you can choose between Oracle, Red Hat, and IBM for Java EE platforms. That's all I meant. It's not a comment on Spring's capability or usefulness. I happen to like it very much. I use it every day and sleep at night. – duffymo Jun 06 '13 at 15:54
  • 1
    @ChristopherYang that's correct. I mean, "Java" would be a vendor lock-n. So eventually, we just need to stop arguing and get something done. :-) – cbmeeks Jul 30 '13 at 16:36
  • @duffymo, But EJB can be distributed between applications using JNDI lookup as they deployed in container, not specific with applications. Can spring do the same? – Jeyan Jun 11 '14 at 09:24
  • 4
    This question is almost five years old. Personally, I think EJBs are dated technology that have lost out in every way to HTTP web services. Simple and open win. Give me REST services and you can keep your EJBs. Spring supports them nicely. That's where the world has gone. – duffymo Jun 11 '14 at 09:27
  • 1
    thanks for the reply. Just looking for the benefits as evaluating the better one(SPRING, EJB 3.x) for migration from existing EJB 2.1 application. One more question, EJB 3.x also supports the WebService. So, could we get the benefit of JNDI/RMI for Java apps distribution and WS for other apps? – Jeyan Jun 11 '14 at 09:37
  • Your insight is awesome yes Oracle tried to freeze JavaEE development but JavaEE Guardians got enought support to get one more version – Ruslan López Sep 09 '16 at 16:59
  • @duffymo, your update that "Spring Boot offers an even better way to write applications without Java EE app servers" could be clarified. If Spring Boot detect Spring MVC controller, it starts an embedded Apache Tomcat (Java EE app server). – Fenix Nov 23 '17 at 12:55
  • 1
    @Fenix - you're correct, but I like it as written. My point is that you don't have to acquire, set up, or start Tomcat. Now I'll correct you - Tomcat is not a full blown Java EE app server. It doesn't let you run EJBs without modification. – duffymo Nov 23 '17 at 14:15
47

First, let me say it clearly, I'm not saying you shouldn't use Spring but, because you are asking for some advantages, here are at least two of them:

  • EJB 3 is a standard while Spring is not (it's a de facto standard but that's not the same thing) and this won't change in the foreseeable future. Although you can use the Spring framework with any application server, Spring applications are locked into both Spring itself and the specific services you choose to integrate in Spring.

  • The Spring framework sits on top of the application servers and service libraries. Service integration code (e.g. data access templates) resides in the framework and is exposed to the application developers. In contrast, the EJB 3 framework is integrated into the application server and the service integration code is encapsulated behind an interface. EJB 3 vendors can thus 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. Another example is clustering support which is transparent to EJB 3 developers.

EJB 3 is not perfect though, it is still lacking some features (e.g. injection of non managed components like simple POJOs).

Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
22

Pascal's points are valid. There are, however, the following in favour of Spring.

  • EJB specification is actually a bit loose, and therefore different behaviours can be observed with different application servers. This will not be true for the most cases, of course, but I've had such a problem for some "dark corners".

  • Spring has a lot of extra goodies, like spring-test, AOP, MVC, JSF integration, etc. EJB has some of those (interceptors, for example), but in my opinion they are not that much developed.

In conclusion, it depends mainly on your exact case.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Maybe something like *Spring only needs a servlet engine* would be more accurate (EJB can be used in any JEE container, servlet engine != JEE container). – Pascal Thivent Nov 22 '09 at 17:40
  • 1
    Well, technically, Spring doesn't require a servlet engine either. For example spring-test uses an in-memory context. – Bozho Nov 22 '09 at 17:55
  • 3
    Well, technically, EJBs don't require a standalone container neither if you go this way. Since EJB 3.1 there is the standard `EJBContainer.createEJBContainer()` API to use an embedded container. So, still, your statement is wrong. – Pascal Thivent Nov 22 '09 at 18:37
  • ok, 3.1 is quite new and obviously I forgot about the additions. Removed that point from my answer. – Bozho Nov 22 '09 at 19:45
-31

Spring is meant to complement EJB, not to replace it. Spring is a layer on top of EJB. As we know, coding of EJB is done using API, which means we have to implement everything in APIs using the Spring framework. We can create boiler-plate code, then just take that plate, add some stuff to it, then everything's done. Internally Spring is connected with EJB -- Spring wouldn't exist without EJB.

The main advantage of using Spring is that there is no coupling at all between classes.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
sasi
  • 1
  • 9
    you are totally wrong, sorry – Jakub H Feb 27 '14 at 09:51
  • 2
    sorry @sasi this is not even close to a correct or nice answer....... – Prakash Apr 02 '14 at 05:35
  • 4
    "Spring wouldn't exist without EJB." Man, are you for real? Have you ever in your life used the "\@Stateless" in the declarion of an EJB, or used \@EJB annotation for injection? Do you think those would work in Jetty or Tomcat? How do you think that transactions are managed under spring? You know that you can deploy a Spring application into a servlet container right? – 99Sono Feb 27 '15 at 16:42
  • Sorry, but you have a not well structured understanding of the both, EJB's as part of Java EE and spring as convention-over-configuration principle. In reality if you do not dig in the deep you may end up thinking that they are some how same thing or related or whatever of that kind. Why? they both have similarities like injection and but the difference between them is huge. It is true Spring was born as alternative to EJB's back in time due to EJB's complexity but it was. Good enough during the Time of Java EE 5 (EJB 3) was retouched also doing like the way spring was doing with their COC – Ishimwe Aubain Consolateur Sep 04 '18 at 13:03