Top
Interstage Studio User's Guide
FUJITSU Software

10.2.2 Using JNDI Lookup to Obtain Objects

Under Java EE, resources and objects can be obtained by means of a Dependency Injection. However, Dependency Injection has the following problems:

The above problems can be resolved by using JNDI lookup to obtain resources and objects in the conventional manner.
Various examples are shown below.

EJB Local Interface Lookup

Perform EJB local interface lookup as shown below.

Lookup Example

InitialContext ic = new InitialContext();
Calc calc = (Calc)ic.lookup("java:comp/env/Calc");

Example of Coding a deployment descriptor for Lookup

<ejb-local-ref>
    <ejb-local-ref-name>Calc</ejb-local-ref-name >
    <local>sample.Calc</local>
    <ejb-link>ejb1.jar#CalcBean</ejb-link>
</ejb-local-ref >

JMS Destination Lookup

Perform JMS Destination lookup as shown below.

Lookup Example

InitialContext ic = new InitialContext();
Queue queue = (Queue)ic.lookup("java:comp/env/jms/myQueue");

Example of Coding a Deployment Descriptor for Lookup

<message-destination-ref>
    <message-destination-ref-name>jms/myQueue</message-destination-ref-name>
    <message-destination-type>javax.jms.Queue</message-destination-type>
    <message-destination-usage>Produces</message-destination-usage>
    <message-destination-link>myDestination</message-destination-link>
</message-destination-ref >
...
<message-destination>
    <message-destination-name>myDestination</message-destination-name>
    <mapped-name>testQueue</mapped-name>
</message-destination>

JDBC Resource Lookup

Perform JDBC resource lookup as shown below.

Lookup Example

InitialContext ic = new InitialContext();
DataSource oracle = (DataSource)ic.lookup("java:comp/env/jdbc/Oracle");

Example of Coding a Deployment Descriptor for Lookup

<resource-ref>
    <res-ref-name>jdbc/Oracle</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <mapped-name>OracleTestServer</mapped-name>
</resource-ref>