Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Looking up an EJB 3.0 EJB

Using EJB 3.0, you can look up an EJB using resource injection (see "Using Annotations") or the `InitialContext` (see "Using Inital Context").

## Using Annotations

Example 19-19 shows how to use annotations and dependency injection to access an EJB 3.0 EJB from an EJB client.

Example 19-19 Injecting an EJB 3.0 EJB in an EJB 3.0 EJB Client

    @EJB AdminService bean;

        public void privilegedTask()
        {
            bean.adminTask();
        }

Example 19-20 shows how to use annotations and dependency injection to access an EJB 2.1 EJB from an EJB 3.0 EJB client.

Example 19-20 Injecting an EJB 2.1 EJB in an EJB 3.0 EJB Client

    @EJB(
        name="ejb/shopping-cart", // optional
        beanName="cart1", // optional
        beanInterface=ShoppingCartHome.class, // optional
        description="The shopping cart for this application" //optional
    )
    private ShoppingCartHome myCartHome;

## Using Inital Context

This section describes:

- Looking Up the Remote Interface of an EJB 3.0 EJB Using ejb-ref

- Looking Up the Remote Interface of an EJB 3.0 EJB Using location

- Looking up the Local Interface of an EJB 3.0 EJB Using local-ref

- Looking up the Local Interface of an EJB 3.0 EJB Using local-location

For more information, see "Configuring the Initial Context Factory".

### Looking Up the Remote Interface of an EJB 3.0 EJB Using ejb-ref

To look up the remote interface of an EJB using an `ejb-ref`:

1. Define an `ejb-ref` for the EJB in the `ejb-jar.xml` file.

   Example 19-21 ejb-jar.xml For an ejb-ref

       <ejb-ref>
           <ejb-ref-name>ejb/Test</ejb-ref-name>
           <ejb-ref-type>Session</ejb-ref-type>
           <local>Test</local>
       </ejb-ref>

   For more information, see "Configuring an Environment Reference to a Remote EJB").

2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

3. Look up the EJB using the `ejb-ref-name` and the appropriate prefix (if required).

   Example 19-22 Looking Up Using ejb-ref in an EJB 3.0 EJB Client Using Initial Context

       InitialContext ic = new InitialContext();
       Cart cart = (Cart)ic.lookup("java:comp/env/ejb/Test");

   For more information, see "Configuring the Initial Context Factory".

### Looking Up the Remote Interface of an EJB 3.0 EJB Using location

To look up the remote interface of an EJB using its `location`:

1. Define the `entity-deployment` attribute `location` in the `orion-ejb-jar.xml` file.

   Example 19-23 orion-ejb-jar.xml For location

       <entity-deployment
           name="Test"
           location="app/Test"
           ...
       >
       ...
       </entity-deployment>

   The default value for `location` is the value of `entity-deployment` attribute `name`.

2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

3. Look up the EJB using the `location`.

   Example 19-24 Looking Up Using location in an EJB 3.0 EJB Client Using Initial Context

       InitialContext ic = new InitialContext();
       Cart cart = (Cart)ic.lookup("java:comp/env/app/Test");

   For more information, see "Configuring the Initial Context Factory".

### Looking up the Local Interface of an EJB 3.0 EJB Using local-ref

To look up the remote interface of an EJB using an `ejb-local-ref`:

1. Define an `ejb-local-ref` for the EJB in the `ejb-jar.xml` file.

   Example 19-25 ejb-jar.xml For an ejb-local-ref

       <ejb-local-ref>
           <ejb-ref-name>ejb/Test</ejb-ref-name>
           <ejb-ref-type>Session</ejb-ref-type>
           <local>Test</local>
       </ejb-local-ref>

   For more information, see "Configuring an Environment Reference to a Local EJB").

2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

3. Look up the EJB using the `ejb-ref-name` and the appropriate prefix (if required).

   Example 19-26 Looking Up Using local-ref in an EJB 3.0 EJB Client Using Initial Context

       InitialContext ic = new InitialContext();
       Cart cart = (Cart)ctx.lookup("java:comp/env/ejb/Test");

   For more information, see "Configuring the Initial Context Factory".

### Looking up the Local Interface of an EJB 3.0 EJB Using local-location

To look up the local interface of an EJB using its `local-location`:

1. Define the `entity-deployment` attribute `local-location` in the `orion-ejb-jar.xml` file.

   Example 19-27 orion-ejb-jar.xml For local-location

       <entity-deployment
           name="Test"
           local-location="app/Test"
           ...
       >
       ...
       </entity-deployment>

   The default value for `location` is the value of `entity-deployment` attribute `name`.

2. Determine whether or not a prefix is required (see "Selecting an EJB Reference").

3. Look up the EJB using the `local-location`.

   Example 19-28 Looking Up Using local-location in an EJB 3.0 EJB Client Using Initial Context

       InitialContext ic = new InitialContext();
       Cart cart = (Cart)ctx.lookup("java:comp/env/app/Test");

   For more information, see "Configuring the Initial Context Factory".