Spring vs. EJB. Can Spring replace EJB?

Since Spring can use transactions in the same way as EJB . For me, Spring can replace the requirement to use EJB. Can someone tell me what are the additional benefits of using EJB?

+79
java spring
Nov 22 '09 at 16:21
source share
4 answers

Spring was designed as an alternative to EJB from the start, so the answer, of course, you can use Spring instead of EJB.

If there is an โ€œadvantageโ€ to using EJB, I would say that it will depend on the skills of your team. If you don't have Spring experience and a lot of EJB experience, then sticking to EJB 3.0 is probably a good move.

Application servers written to support the EJB standard could theoretically be migrated from one compatible Java EE application server to another. But this means that you must stay away from any specific suppliers that block you from one supplier.

Spring is easy to port between application servers (e.g. WebLogic, Tomcat, JBOSS, etc.) because it is independent of them.

However, you are locked out in Spring.

Spring encourages good OO design methods (such as interfaces, layers, problem separation) that benefit any problem they touch, even if you decide to switch to Guice or another DI framework.

Update. This question and answer is five years in 2014. I must say that the world of programming and application development at that time changed a lot.

This is no longer just a choice between Java or C #, Spring or EJB. With vert.x, Java EE can be abandoned altogether. You can write a very scalable, polyglot without an application server.

Update: Now March 2016. Spring Boot offers an even better way to write applications without Java EE application servers. You can create an executable JAR and run it on the JVM.

I wonder if Oracle will continue to support the Java EE specification. Web services switched to EJB. EJB's decision is dead. (Only my opinion.)

+169
Nov 22 '09 at 17:13
source share

First, let me put it clearly, I am not saying that you should not use Spring, but since you are asking for some benefits, here are at least two of them:

  • EJB 3 is standard, and Spring is not (it is a de facto standard, but it is not the same thing), and this will not change in the foreseeable future. Although you can use the Spring framework with any application server, Spring applications are blocked both in Spring itself and in certain services that you want to integrate into Spring.

  • The Spring framework sits on top of application servers and service libraries. Service integration code (for example, data access patterns) is in the structure and is provided to application developers. In contrast, the EJB 3 framework is integrated into the application server, and the service integration code is encapsulated behind the interface. In this way, EJB 3 vendors can optimize developer productivity and experience by working at the application server level. For example, they can closely link the JPA engine to JTA transaction management. Another example is clustering support, which is transparent to EJB 3 developers.

EJB 3 is not perfect, although it still does not have some features (for example, injection of unmanaged components such as simple POJOs).

+40
Nov 22 '09 at 16:40
source share

Pascal points are valid. However, Spring includes the following.

  • The EJB specification is actually a bit loose, and therefore different behaviors can be observed on different application servers. Of course, for most cases this will be wrong, but I had such a problem for some "dark corners".

  • Spring has many additional useful properties, such as spring-test, AOP, MVC, JSF integration, etc. EJB has some of these (for example, interceptors), but, in my opinion, they are not so developed.

In conclusion, it depends mainly on your particular case.

+16
Nov 22 '09 at 16:51
source share

Spring is intended to complement EJB, not to replace it. Spring is a layer on top of EJB. As you know, EJB coding is performed using the API, which means that we must implement everything in the API using the Spring structure. We can create a boiler plate code, and then just take this plate, add something to it, and then do everything. Inner Spring is associated with EJB - Spring will not exist without EJB.

The main advantage of using Spring is that there is no relationship between classes.

-22
Feb 27 '14 at 9:46
source share



All Articles