Difference between Java Bean and Enterprise Java Beans? [closed] Difference between Java Bean and Enterprise Java Beans? [closed] java java

Difference between Java Bean and Enterprise Java Beans? [closed]


A JavaBean is just a plain old Java object that conforms to certain conventions including the use of accessor functions (getFoo/setFoo) for member access, provision of a default constructor and a few other things like that.

An Enterprise JavaBean is a component in a Java EE application server which comes in several flavours, the details of which vary by which version of Java EE you're talking about (or, more specifically, which specific set of EJB specifications are involved).

JavaBeans were originally mostly intended to be used in builder tools by providing a known interface which could be looked for through introspection in the tools. They quickly turned into what amounts to a religion, however.

Enterprise JavaBeans are intended to provide encapsulated business logic for enterprise applications within a general container that provided things like session management, security, resource pooling, etc. as services thus allowing the business logic to be (relatively) untainted by these cross-cutting concerns. (Whether or not they accomplished this is a matter that is up for debate, given how difficult they were to use at first. More recent versions of the specification have made this easier, however. Legacy apps, though, are still a pain and sadly likely the majority of the EJBs you're likely to encounter.)


Edited to add:


A Java Bean is defined as an instance of a class that contains private attributes (data), and getter & setter methods.

If you have:

private String myString; in your class, you should have the methods public String getMyString (); and public void setMyString (String settingString); defined in your code. Although, I have found that it is not absolutely necessary to have everything defined, just don't be surprised if something breaks!

An EJB (Enterprise Java Bean) is much more complex They only reside in application servers that handle EJBs (Tomcat doesn't hold EJBs). There are 3 types of EJBs:

  1. Session: Usually contain some business logic.
  2. Entity: Usually interface with a data store (such as a database).
  3. Message-Driven: Receives messages from JMS.


Java beans refers to classes with only fields and their getter-setter methods. With little or preferably no business logic at all. They are also known as POJO (Plain Old Java Object).

EJB's are part of J2EE specification that can be used to leverage full functionality of J2EE compliant servers, such as transactions, session management, security etc. (That does not mean that you cannot use these without using EJBs)