Accessing session beans using dependency injection

You can access session beans either using the @EJB annotation (which injects the bean in the client class) or by performing the Java Naming and Directory Interface (JNDI) lookup. EJB containers are required to make JNDI URLs of the EJBs available to clients.

Injecting session beans using @EJB works only for managed components, that is, components of the application whose life cycle is managed by the EJB container. When a component is managed by the container, it is created (instantiated) and destroyed by the container. You do not create managed components using the new operator. JEE-managed components that support direct injection of EJBs are servlets, managed beans of JSF pages, and EJBs themselves (one EJB can have another EJB injected into it). Unfortunately, you cannot have a web container inject EJBs in JSPs or JSP beans. Furthermore, you cannot have EJBs injected into any custom classes that you create and that are instantiated using the new operator. Later in the chapter, we will see how to use JNDI to access EJBs from objects that are not managed by the container.

We could use a student bean (created previously) from a managed bean of a JSF as follows:

import javax.ejb.EJB; 
import javax.faces.bean.ManagedBean; 
 
@ManagedBean 
public class StudentJSFBean { 
  @EJB 
  private Student studentEJB; 
} 

Note that if you create an EJB with no-interface view, then all public methods in that EJB will be exposed to the client. If you want to control the methods that could be called by the client, then you should implement a business interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.166.149