Session Bean Implementations

To implement the local stateless bean

  1. import javax.ejb.Stateless;
  2. add the annotation above the class: @Stateless  to see what is the difference between beans check my Choosing and developing a Session Bean

To implement the remote statelful bean

  1. import javax.ejb.Statelful;
  2. add the annotation above the class: @Stateful

To implement the local singleton bean

  1. import javax.ejb.LocalBean;
  2. import javax.ejb.Singleton;
  3. add the annotation above the class:
    1. @LocalBean
    2. @Singleton

Every session bean has:

  1. Business interface
  2. Implementation class
  3. Deployment descriptor information

Developing a Session Bean

Class requirements are similar to JavaBeans:

  1. Must be a top level class
  2. Must be defined as public
  3. Can not be final or abstract
  4. Must have a public no-arguments constructor that takes no parameters
  5. Must not define the finalize method
  6. Must implement the methods of the business interface


Enum serves as a type of fixed number(meaing limited to certain number) of constants and can be used at least for two things:


public enum Month {


This is much better than creating a bunch of integer constants.

creating a singleton

public enum Singleton {

   // init



Choosing and developing a Session Bean

Use a session bean if:

  1. Only one client has access to bean at any given time
  2. State of bean is not persistent
  3. Bean represent a web service

Use a Stateful Session bean if:

  1. Bean state represent client interaction
  2. Bean needs to hold client data across interactions
  3. Bean acts as a client mediator to other beans
  4. Need thread-safe interactions

Session Bean cardinality(taken from DevelopIntelligence)

bean cardinality.PNG

From the diagram, you can see that Stateless and Singleton beans looks very similar when it comes to Client-bean instances, except that in singleton all the clients access single object while in Stateless some objects are uses by clients and returned back to pool.

Developing a Session Bean

Steps to create a session bean:

  1. Define business logic interface
  2. Annotate the business interface
  3. Create the session bean, implementing the interface
  4. Annotate class defining type
  5. Compile, deploy and debug as discussed above

Dependency injection in EJB

After an EJB is instantiated inside the Java EE container, but before it is handed out to a client, the container may initialize property data on the instance according to rules defined for that enterprise bean. This feature is called dependency injection, an external provider initializes the properties of an object instance instead of by the class itself.

■Note Injection uses a “push” model to push data out to the bean, and it occurs regardless of whether the bean actually uses the data. If there is a chance that the data will not be used, the bean may elect to avoid incurring the cost of the resource derivation by performing a Java Naming and Directory Interface (JNDI) lookup in Java code to “pull” the
data, only if it is actually (or likely to be) used.

Common examples of dependency injection use in EJB are as follows:
• Injecting an EntityManager into a session bean for interacting with entities in a persistence unit
• Injecting a UserTransaction into a session bean that manages its transaction demarcation

Even though, the video below is for Spring framework, which is actually EJB’s alternative, it covers the Dependency Injection(DI) pretty well.