Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

EJB and Spring

843830Dec 21 2005 — edited Jan 5 2006
Hi,

As I know, the Spring framework is not just a container for POJOs. However,I usually see articles that describe the comparison between EJB and Spring. Why? I am curious about this.

EJB is just a specification of J2EE. The EJB runs in a EJB container in the J2EE environment.
Spring uses and wires lots of J2EE specifications together such as JSP/Servlet/JaveBean/JDBC to provide the service in the J2EE environment. Also, it could uses the EJB.

So, why does we want to compare EJB to Spring?

Thank you for your advice.

Comments

800487
Refer
http://www.onjava.com/pub/a/onjava/2005/06/29/spring-ejb3.html
843830
Sorry, I mean EJB2.1, not EJB3.0.
843830
EJB is just a specification of J2EE. The EJB runs in
a EJB container in the J2EE environment.
Spring uses and wires lots of J2EE specifications
together such as JSP/Servlet/JaveBean/JDBC to provide
the service in the J2EE environment. Also, it could
uses the EJB.

So, why does we want to compare EJB to Spring?
Spring and EJB are compared in terms of persistence. Spring (which uses Hibernate for persistence) is lightweight compared to EJB. There are other features like transaction management, scalability which could be compared. The basis of all these articles is "To EJB or not to EJB"... an attempt to provide an alternative to the bulkiness and complexity of EJB. EJB 3.0 is a promising specification which incorporates some of the features to make it lightweight. Read the article in that reply, if you are interested.
796254
Hi,

As I know, the Spring framework is not just a
container for POJOs. However,I usually see articles
that describe the comparison between EJB and Spring.
Why? I am curious about this.
Because Spring came into being because its original developer found EJBs too complex and heavyweight to be useful. Spring is a framework that makes it possible to do JEE without EJBs.
EJB is just a specification of J2EE.
This is incorrect. JEE includes JDBC, JSP, JMS, JTA, JNDI and a host of other technologies besides EJBs. It's possible to be a JEE application without EJBs.

The EJB specification is just that - a specification for a particular technology under JEE.
The EJB runs in a EJB container in the J2EE environment.
Correct.
Spring uses and wires lots of J2EE specifications
together such as JSP/Servlet/JaveBean/JDBC to provide
the service in the J2EE environment. Also, it could
uses the EJB.
You can remote to EJBs if you wish, but you don't have to write EJBs of your own. Spring almost treats EJBs as legacy code, like mainframes.
So, why does we want to compare EJB to Spring?
See above.
Thank you for your advice.
%
843830
Because Spring came into being because its original developer found EJBs too complex and heavyweight to be useful. Spring is a framework that makes it possible to do JEE without EJBs.
I think their scopes are totally different. It still is strange to compare them to me.
The EJB is one of all specifications of J2EE. Spring is not the specification of J2EE. Spring has its own specifications as a framework to glue all specifications including EJBs of J2EE to provide the J2EE services. Am I right?
796254
Because Spring came into being because its original
developer found EJBs too complex and heavyweight to
be useful. Spring is a framework that makes it
possible to do JEE without EJBs.

I think their scopes are totally different. It still
is strange to compare them to me.
You're right - Spring is more encompassing than EJBs.
The EJB is one of all specifications of J2EE. Spring
is not the specification of J2EE. Spring has its own
specifications as a framework to glue all
specifications including EJBs of J2EE to provide the
J2EE services. Am I right?
That's correct. There's no specification for Spring - it's the brainchild of Rod Johnson and the team that's coalesced around him. Spring can glue EJBs into an app and more besides.

%
843830
I think their scopes are totally different. It still
is strange to compare them to me.
The EJB is one of all specifications of J2EE. Spring
is not the specification of J2EE. Spring has its own
specifications as a framework to glue all
specifications including EJBs of J2EE to provide the
J2EE services. Am I right?
You are right. All those articles are aimed at "to EJB or not to EJB".
843830
It seems that the POJOs in Spring in J2EE replaces the EJBs in J2EE.
If it is true, why couldn't it be the POJOs in J2EE without Spring?

EJBs in J2EE environment use JTA, JDBC, JMS, and so on.
POJOs in Spring in J2EE environment use JTS, JDBC, JMS, and so on.
POJOs in J2EE environment use JTS, JDBC, JMS, and so on.

What is the difference among three ways above to implement the business logic to provide the enterprise services?

Thank you for your guiding.
796254
It seems that the POJOs in Spring in J2EE replaces
the EJBs in J2EE.
If it is true, why couldn't it be the POJOs in J2EE
without Spring?
Can be, but what do you lose by doing this?
EJBs in J2EE environment use JTA, JDBC, JMS, and so on.
EJBs have the added benefit of having the container manage their life cycle.
POJOs in Spring in J2EE environment use JTS, JDBC, JMS, and so on.
Add in that Spring encourages the use of interfaces and externalizes configuration in XML files. I think Spring's use of singletons is meaningful here. It's tied in with the use of interfaces.

There's a lot added to the Spring framework classes that does things for you behind the scenes. That's what's different.
POJOs in J2EE environment use JTS, JDBC, JMS, and so on.
But in this case you either end up hardwiring dependencies or externalizing them yourself.
What is the difference among three ways above to
implement the business logic to provide the
enterprise services?
See above. If you do POJOs yourself, you're writing all that framework code. You can. It's your choice whether to do it or not.

%
843830
Thank you very much.

If I write an BMP bean + JDBC API in J2EE, a POJO + JDBC API in Spring in J2EE, and a POJO + JDBC API in J2EE, could you please guide me what the real different things that needed to be done are among them?

Thank you.
796254
Thank you very much.
I could do without the bold.

I'm not sure exactly what you're looking for below. I'm certainly not going to give you full examples of each. But here goes:
If I write an BMP bean + JDBC API in J2EE,
You'll have to write the JDBC code in your entity bean that persists the entity bean attributes into tables. You'll need CRUD operations for your entity.
a POJO + JDBC API in Spring in J2EE,
If I had to persist a POJO named Foo, I'd start with a FooDao interface:
public interface FooDao
{
   public Foo findById(Long id);
   public List findAll();
   public void saveOrUpdate(Foo foo);
   public void delete(Foo foo);
};
My Spring implementation of the FooDao would extend JdbcTemplate and use all the machinery it made available to me. I'd have to write a dao application context XML to wire it together with its connection, etc.
and a POJO + JDBC API in J2EE,
Same as above, without the JdbcTemplate to help me.

could you please guide me what the real
different things that needed to be done are among
them?

Thank you.

Is that what you had in mind?

%
1 - 11
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Feb 2 2006
Added on Dec 21 2005
11 comments
169 views