EJB3+Tutorial+1+-+Create+Session+Bean

Now that we have all the preliminary setup of the environment we next need to create a session bean.

The Session Bean
The basic requirement for a session bean is that it must implement an interface and be either annotated with **@Stateless** or be registered in an XML file. We are sticking strictly to using annotations. The annotation goes on the class and not the interface, so here's the interface.

First we create a session bean. Here is one such example: code format="java5" package service;
 * The Interface**

/** * A requirement for EJB3 Session beans is that they implement an interface. * This interface does not specify whether it is local or remote so we won't * know until it is used which way to treat it. One convention is to include the * name "Local" or "Remote" in the name of the service. */ public interface HelloWorldService { void sayHelloTo(final String name); } code

To create this file,
 * 1) select your **src** directory, right-click and select **New:Interface**.
 * 2) For **Name**, enter **HelloWorldService**
 * 3) For **Package** enter **service**
 * 4) Click on **Finish** then enter the above code into the file
 * 5) Save your file (ctrl-s)

Next, we need to create a session bean. Here's the code for it: code format="java5" package service.impl;
 * The Session Bean**

import javax.ejb.Stateless;

import service.HelloWorldService;

/** * I'm a stateless session because of the -at- Stateless annotation. I only * implement one interface and that interface does not explicitly declare itself * to be either -at- Local or -at- Remote. Therefore, I am a Stateless session * bean with a local interface. */ @Stateless public class HelloWorldServiceImpl implements HelloWorldService {

public void sayHelloTo(final String name) { System.out.printf("Hello to %s\n", name); }

} code

Notice that this class has the @Stateless annotation. The container will find this class and register it automatically under JNDI using the (non-package qualifited) class name plus "/local". In this example, that means we'll need to lookup **"HelloWorldServiceImpl/local"**.

This class is obviously stateless because of the annotation. This is the default behavior. However, using the annotation will get it automatically available from JNDI. (We could put this information in an XML file and get the same results.)

This class is also local. By default, session beans are local (no RMI-IIOP to call them) unless:
 * They implement more that one interface (ignoring common interfaces like Serializable and Comparable).
 * There is a @Remote annotation

If you still want a local session bean where there is more than one interface, you can use @Local.

To create this file:
 * 1) select your **src** directory, right-click and select **New:Class**
 * 2) For **Name**, enter **HelloWorldServiceImpl**
 * 3) For **Package** enter **service.impl**
 * 4) Click on **Finish** then enter the above code into the file
 * 5) Save your file (ctrl-s)