Java EE/EJB 3 vs. Spring

March 1, 2007

I was collecting some opinions on when to choose Java EE over Spring (or vice versa). I know that one doesn’t have to choose, but I still believe most of the time only one of them (and not both) will become a core part of your architecture.

Here’s my preliminary list of resources:

I used the Spring IoC container once, and was very happy with it. I used EJB 2.x, and it sucked (but not as much as many claim), and EJB 3 is a vast improvement. I’m not leaning towards one or the other. At the moment, I see these main differences:

Any pointers to things I’m obviously missing?

About

This page contains a single entry from Stefan Tilkov's Random Stuff posted on March 1, 2007 4:21 PM. The previous post in this blog was Markdown in JavaScript. The next post in this blog is Animation of a Samuel L. Jackson Pulp Fiction speech in type. Many more can be found on the main index page or by looking through the archives.

Comments

If you are comparing Spring to EJB 3, you really should take a look at the JBoss Seam framework. The latest version also provides Spring integration. http://jboss.com/products/seam

Yes, I do work for JBoss.

Kevin

Posted by: Kevin Barfield at March 1, 2007 5:32 PM | link

Very valid comment, unfortunately probably not applicable in my client’s particular situation (for non-technical reasons).

Posted by: Stefan Tilkov at March 1, 2007 5:45 PM | link

Well, I am biased, so I say use Spring :). But here are some remarks:

  1. Spring is not an alternative for Java EE. Rather, it builds on top of Java EE, by making the JEE libraries less verbose to use, and by solving common programming errors. Spring actually supports EJB3 features, especially the nice Java Persistence API. BEA´s WebLogic actually uses Spring to implement the EJB 3.0 interception and injection model. See http://blog.interface21.com/main/2007/02/11/weblogic-10-tech-preview-ships-builds-on-spring-framework/

  2. Spring is not a “package deal”. You can use the IoC container without using Spring WEB MVC, for instance. You can choose to use the JpaTemplate , but you can also use the EJB3 Java Persistence API directly, by injecting an EntityManager. It´s all about choice.

  3. Spring does not “heavily rely” on using XML for configuration. It it the most popular format, but there are other formats, like Properties files, or the new Java configuration format, described here: http://blog.interface21.com/main/2006/11/28/a-java-configuration-option-for-spring/.

  4. One of the core ideas about Spring is that you keep it out of your architecture. Especially in the service layer and in your domain objects (the most important part of your application), you are not expected to use any Spring imports, or rely on Spring classes. Dependency Injection combined with externalized meta-data helps a lot with this true POJO style of development. In JEE, the two are often mixed because of the heavy usage of annotations (is an annotated object still a POJO?)

  5. Currently, there aren´t a lot of Java EE app servers out there. Sure, there is Glassfish, JBoss 5, and WebLogic 10, but most of them are not in their preview stages. And I think that IBM has not even announced its support for JEE 5 yet.

Cheers,

Arjen

Posted by: Arjen Poutsma at March 1, 2007 6:15 PM | link

Most review that I’ve read lists Spring’s XML configuration as a negative factor and hail EJB 3’s annotation as an improvement. Weird, I like XML better than annotations. ;-) It must have something to do with the variety of legacy java apps that I maintain that don’t support annotation but it could also be because I like XML’s structured nature better. :-)

Posted by: Eric at August 18, 2007 8:34 AM | link