EJB 3 Session Beans

The Business Interface

A stateless session business interface is a standard Java interface that does not extend any EJB-specific interfaces. This interface has a list of business method definitions that will be available for the client application. Every session bean must have a business interface that can be implemented by the bean class, generated at design time by tools such as Oracle JDeveloper, NetBeans, or Eclipse; or generated at deployment time by the EJB container.

Business interfaces can use annotations as well, as described in the following list:

The @Remote annotation can be used to denote the remote business interface.

The @Local annotation can be used to denote the local business interface.

If no annotation is specified in the interface, then it is defaulted to the local interface.

If your architecture has a requirement whereby the client application (web application or rich client) has to run on a different JavaVirtual Machine (JVM) from the one that is used to run the session beans in an EJB container, then you need to use the remote interface. The separate JVMs can be on the same physical machine or on separate machines. If your application architecture is going to use the same JVM for both the client application and the session beans, then use the local interface.

It is possible that your application architecture requires both remote and local interfaces. For example, an enterprise might have an order entry application that is developed using session beans that have business methods for submitting new orders and also addressing administrative tasks, such as data entry for the products. Potentially, you could have two different client applications that access the back-end order entry application, as follows:

A web client application (as shown in Figure 3) that can be run in the same JVM as the session bean and used to submit new orders

A rich client application (as shown in Figure 4) that runs on an end-user desktop machine and is used by the administrator for data entry purposes

Listing 2 shows the code snippet for the SearchFacade remote business interface, with an @Remote annotation and a wineSearch() method declaration. The wineSearch() method takes one parameter that represents the type of the wine, and returns a list of wines that match the wine type criteria.

Business Methods

The methods implemented in the bean class must correspond to the business methods declared in the remote or local business interfaces. They are matched up based on the convention that they have the same name and method signature. Other methods in the bean class that do not have the corresponding declaration in the business interfaces will be private to the bean class methods.

The SearchFacade bean implements one method, wineSearch(), which has been declared in both remote and local business interfaces. The wineSearch() method returns a static wines list based on the type of wine. Listing 4 shows the implementation for wineSearch().

Dependency Injection

In this section, you will look into using dependency injection in stateless session beans.

EJB 3 containers provide the facilities to inject various types of resources into stateless session beans. Typically, in order to perform user tasks or process requests from client applications, the business methods in the session bean require one or more types of resources. These resources can be other session beans, data sources, or message queues.

The resources that the stateless session bean is trying to use can be injected using annotations or deployment descriptors. Resources can be acquired by annotation of instance variables or annotation of the setter methods. Listing 5 shows an example of setter and instance variable.based injection of myDb, which represents the data source.

Listing 5. Data Source Injection

@Resource
DataSource myDb;

or

@Resource
public void setMyDb(DataSource myDb) {
this.myDb = myDb;
}

You typically use the setter injections to preconfigure or initialize properties of the injected resource.