Application Techniques



Using EJB Bean References

How to create a bean reference in the deployment descriptor and look it up at runtime.

About this technique

Details

Category

Enterprise JavaBean Techniques

Description

You'll learn about:

Related reading

See the part on developing Enterprise JavaBeans in the Programmer's Guide

Specifying bean references in the deployment descriptor   Top of page

To use the JAR Designer to add a bean reference to the deployment descriptor, you add the reference entry and specify its properties as shown here:

The information in the deployment descriptor includes the following:

Property value

Description

Name

This is the name that is used in the code that is doing the lookup. The EJB specification recomments that you preface this name with ejb/

At deployment time, the container maps this name to the appropriate EJB class

Referenced bean name

This is the name of the bean that you want to access as it is referred to within the EJB JAR. It is not bean's class name

Bean type

Can be entity or session

Home interface name

The fully qualified package name of the referenced bean's home interface

Remote interface name

The fully qualified package name of the referenced bean's remote interface

How the SBBankATM finds bean references   Top of page

The SBBankATMBean finds out about the entity beans that it manages in the initializeSessionBean() custom method. To get the bean references, the intializeSessionBean() method does the following:

If it is not able to get find one of the bean references, the method throws an EJBException which is an unrecoverable error.

Here's the code that does a lookup for the EBCustomer bean and the EBAccount bean:

  try  
    { 
     m_initialContext = new javax.naming.InitialContext(); 
     Context contextEnv =  
       (Context) m_initialContext.lookup("java:comp/env");	 	 	  
     } 
  // The Environment name specified in the EJB jar must match the //name below. 
     Object objEntityBeanLookup = (Object)    contextEnv.lookup("/ejb/EBCustomer"); 
     m_EBCustomerHome = (EBCustomerHome)  
       PortableRemoteObject.narrow(objEntityBeanLookup,  
        com.examples.bankdemo.EBCustomerHome.class);	 	  
   
  // Then, get our EBAccountHome 
      objEntityBeanLookup = (Object) contextEnv.lookup("/ejb/EBAccount");	 	 	  
      m_EBAccountHome = (EBAccountHome) 
      PortableRemoteObject.narrow(objEntityBeanLookup,      com.examples.bankdemo.EBAccountHome.class); 
  } 
  catch (Exception e) 
  { 
       String sErrorMessage = e.toString(); 
  //This is an unrecoverable error, so throw an EJBException 
     throw new EJBException(sErrorMessage);} 
  } 





Copyright © 2000, SilverStream Software, Inc. All rights reserved.