Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJBs and Modern Java Development

I'm new to Java EE and see that EJBs are alive and well within the pure Java/Oracle community. However everyone at work makes a disgusted look on their face whenever someone else even utters the phrase "EJB", which makes me think that they're either becoming extincted or have been replaced by modern development teams with some other middleware technology.

Just like JSP has given way to JSF-centric view technologies, is the same true for EJBs? Either way, what are some popular alternatives to EJBs and how are they different? What benefits or features do they offer over EJBs?

like image 230
IAmYourFaja Avatar asked Jan 09 '12 16:01

IAmYourFaja


People also ask

What is the replacement for EJB in Java?

The Spring Framework is an application framework and IoC container for the Java platform. The framework was initially created as an alternative to EJB.

Why is EJB not used anymore?

For simpler web applications that can run inside less complex servlet containers like Tomcat, you wouldn't use EJB. The problem with EJBs is that they still bear all the blame they gained with early versions - most of which they honestly deserved , so nowadays they aren't so much popular.

Why EJB is used in Java?

EJB is a server-side software element that summarizes business logic of an application. Enterprise Java Beans web repository yields a runtime domain for web related software elements including computer reliability, Java Servlet Lifecycle (JSL) management, transaction procedure and other web services.

What is 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.


2 Answers

Your coworkers might have seen only EJB2 and earlier, which indeed were unholy beasts that very few people enjoyed using.

In EJB2, For the simplest of things very invasive framework provided interfaces had to be implemented, with insane life cycle methods for which the developer was required to provide an implementation, but which had nothing to do with the (business) goals of the developer. A few of such artifacts had to be provided.

Additionally every bean had to be kept in sync with entries in a very verbose and hard to read deployment descriptor (an XML file). As if that wasn't insulting enough to a developer, special tools had to be used to 'enhance' the bean and to generate proxy classes, skeletons and stubs. Common OO things like inheritance wasn't supported. There was a kind of injection, but a weird one that existed of putting things in a kind of map (directory actually) associated with each bean.

The original EJB 1 model enforced all communication to be remote and all objects to be distributed (EJBs were originally seen as a remoting technology only). So to get the benefits of EJB you were also forced to make your architecture distributed, even if this was completely unnecessary.

Perhaps the biggest insult of all was the concept of the Entity Bean (not to be confused with JPA entities). The faults with this type of bean were so great that even the biggest supporters of EJB at the time could barely recommend it to anyone.

Then as a very practical problem, compatibility between EJB implementations was not very good to say the least. Especially Entity Beans required massive amounts of vendor specific configuration.

To top it all off, EJB implementations required heavyweight (in terms of installed size and required memory) application servers, which were closed source and rather expensive (thus preventing companies from upgrading or switching because the investment had to be first paid back). I can't remember many people blogging about the technology at the time, and as far as I remember the technology was mostly pitched by sales teams to managers of big corporations.

It's not surprising that the technology wasn't really loved by the average developer.

Sun just in time became aware that the technology was heading in a completely wrong direction and did a 180° turn and started a massive re-engineering effort.

As a result, EJB 3 (2006) is a very sane and lightweight approach. There are no required framework interfaces to implement, there is no required XML, only a single bean has to be coded (just the simple POJO), there are sane defaults everywhere (convention over configuration), no special tools are required (normal javac will do), and using simple beans locally is actually the common case now.

Entity Beans were so flawed that they were dropped completely and replaced by the much saner approach advocated by TopLink and Hibernate among others.

Combine this with a wide availability of free, lightweight and open source implementations, in combination with many well known bloggers advocating the technology (e.g. Adam Bien, Rezha Rahman, Gavin King) and the rise back into popularity is easy to explain.

There have been a number of "Spring to Java EE" migration guides published recently, and those got a very favorable number of votes at the various news sites with many people expression their support for EJB as being a very good technology now. This would have been unthinkable half a decade ago (when EJB 3 was just released and wasn't very known yet).

The most common alternative to EJB is Spring Core (Spring Beans). At the moment I think there are no definite big advantages Spring has over EJB and the other way around. The two technologies are very similar. Spring offers some more convenience utilities, while EJB is conceptually more lightweight (no XML). Both advantages are somewhat subjective. They typically are inspired by each other's functionality and who is slightly ahead often depends on which technology has last released a major new version.

like image 128
Arjan Tijms Avatar answered Sep 21 '22 13:09

Arjan Tijms


The first version of EJB was introduced back in the end of the 1990s.

The disgusted look on your coworkers face when EJB is mentioned could be the consequence of the very complicated and troublesome usage patterns of the first two versions of EJB (e.g. complex xml deployment descriptors).

3.0 and 3.1 were major improvements regarding ease of use.

A Popular alternative is Spring framework + Hibernate / JPA

like image 42
bpgergo Avatar answered Sep 21 '22 13:09

bpgergo