Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB 3.1 or Spring 3.. When to choose which one?

EJB achieved many improvements in 3.x versions, Spring is also commonly used and version 3 is a good alternative.

There are many articles on web, but no exact comparison about ejb3x versus spring3x.. Do you have any ideas about them, in real world examples which one is better at which conditions?

For example, we want to separate db and server, which means our application will be on a server, our database will be in another server.. EJB remoting vs Cluster4Spring etc ?

Doing everyting @Annotation is always good? configuration never needed?

like image 547
asyard Avatar asked Aug 16 '11 09:08

asyard


People also ask

Which is better EJB or 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.

Is EJB still used in 2022?

EJB is still there and growing up. There are many new features (SOAP/RESTful webservice, JPA entities, JAXB...)

What is the difference between Spring and EJB?

Key Differences between EJB and Spring. EJB vs Spring's main difference is that EJB is a specification of Java EE, whereas Spring is a framework or an implementation. EJB is an architecture for transactional, component-based programming. It makes server-side development much easier for the Java developer.

Is Spring alternative to EJB?

The Spring Framework is an application framework and IoC container for the Java platform. The framework was initially created as an alternative to EJB. Spring offers modular approach to adding new functionalities, which means that developers can use only parts they're interested in.


2 Answers

For your use case where the application runs on one server and the database runs on another, the choice between EJB and Spring is irrelevant. Every platforms supports this, be it a Java SE application, a simple Servlet container like Tomcat or Jetty, PHP, Ruby on Rails, or whatever.

You don't need any kind of explicit remoting for that. You just define a datasource, provide the URL where your DB server lives and that's it.

That said, both EJB and Spring Beans do make it easier to work with datasources. They both help you defining a datasource, injecting it in beans and managing transactions associated with them.

Of the two, EJB (and Java EE in general) is more lightweight and adheres more to the convention over configuration principle. Spring requires more verbosity to get the same things and depends a lot on XML files which can quickly become very big and unwieldy. The flip side of the coin is that Spring can be less magical and you might feel more in control after having everything you want spelled out.

Another issue is the way EJB and Spring are developed.

EJB is free (as in free beer), open-source and non-proprietary. There are implementations of EJB being made by non profit organizations (Apache), open source companies (Redhat/JBoss) and deeply commercial closed source enterprises (IBM). I personally would avoid the latter, but to each his own.

Spring on the other hand is free and open-source, but strongly proprietary. There is only one company making Spring and that's Springsource. If you don't agree with Rod, then tough luck for you. This is not necessarily a bad thing, but a difference you might want to be aware of.

Doing everyting @Annotation is always good? configuration never needed?

It's an endless debate really. Some argue that XML is hard to maintain, others argue that annotations pollute an otherwise pure POJO model.

I think that annotating a bean as being an EJB stateless bean (@Stateless) or a JPA entity (@Entity) is more cleanly done using annotations. Same goes for the @EJB or @Inject dependency injections. On the other hand, I prefer JPQL named queries to be in XML files instead of annotations, and injections that represent pure configuration data (like a max value for something) to be in XML as well.

In Java EE, every annotation can also be specified in XML. If both the annotation and the XML equivalent are present, the XML overrules the annotation. This makes it really convenient to start with an annotation for the default case, but override it later via XML for a specific use case.

The current preference in Java EE seems to be more towards (simple) annotations combined with a large amount of convention over configuration.

like image 169
akira Avatar answered Nov 10 '22 17:11

akira


The real question you should be asking is CDI/EJB or Spring

like image 33
David Blevins Avatar answered Nov 10 '22 18:11

David Blevins