Creating EJB 3.1 Stateless Session Bean using JBoss 6.1 Example

Stateless session beans are stateless in nature, in other words it does not remember its previous invocations. A user will call the bean and the bean will return a result. A stateless bean is not good for maintaining the state of an interactive session such as required to maintain a list of purchases. However, it is useful for one-time calculations

This tutorial demonstrates creating EJB using @Stateless annotation. The mappedName attribute of @Stateless annotation describe a user friendly-name used to invoke bean.

A session bean can also have a local, remote and no-interface client view. The interface used determines the intent and scope of the bean. The no-interface view is new to EJB 3.1. This approach allows the developer to use EJB without having to declare a business interface.

Accessing a Session Bean using Dependency Injection
There are two techniques for gaining access to an EJB:

  • Dependency Injection (DI)
  • Java Naming and Directory Service (JNDI).

DI is the easiest when we can use it, but JNDI can be used in places where DI is not supported. We will look at both of these approaches. However, EJBs should not be created using the Java new keyword.
If the EJB is created using this keyword, then it will no longer be an EJB but rather a regular object. It will not be able to take advantage of the support provided by the EJB container.

To use DI, use @EJB annotation. This will inject EJB reference into our application. Following code demonstrate use of @EJB.

Now create a ear file containing Servlet, EJB and deploy it on JBoss’s root directory. Output will be similar to following screen shot.

Download Source

About the Author

has written 69 posts.

I Love Coding, Analyzing, Designing and doing it the right way. I am obsessed about learn anything new daily and sharing what I have learn each Day. This Blog is journey of my Zeal, going through step by step to the mountain of desire to become prefect. Google Wanna Hire me ?

Copyright © 2020 All rights reserved.
Proudly powered by WordPress. Developed by 7Tech Solutions