Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.5.0) Part Number E13981-01 |
|
|
View PDF |
You can directly look up a bean instance from JNDI (or use resource injection in an EJB 3.0 EJB client) and retrieve a bean instance without the home interface. If the <home>
or <local-home>
element is removed from an EJB reference, a bean instance is returned from JNDI instead of the home.
The bean instance is created by executing the no-argument create
method on the home interface. Stateful session beans and entity beans can also use this shortcut, but they must have a no-argument create
method, or otherwise an exception will be thrown at lookup time.
In both cases, the syntax used in obtaining the reference to the EJB business interface is independent of whether the business interface is local or remote. In the case of remote access, the actual location of a referenced enterprise bean and EJB container are, in general, transparent to the client using the remote business interface of the bean.
Using EJB 3.0, you can look up an enterprise bean using resource injection (see "Using Annotations") or the InitialContext
(see "Using Initial Context").
Alternatively, you can define an environment reference to an EJB 3.0 bean using OC4J-proprietary annotations or deployment XML (see "EJB Environment References").
Example 29-3 shows how to use annotations and dependency injection to access an EJB 3.0 enterprise bean from an EJB client.
This section describes the following:
Looking Up the Remote Interface of an EJB 3.0 Enterprise Bean Using ejb-ref
Looking Up the Remote Interface of an EJB 3.0 Enterprise Bean Using location
Looking up the Local Interface of an EJB 3.0 Enterprise Bean Using local-ref
Looking up the Local Interface of an EJB 3.0 Enterprise Bean Using local-location
For more information, see "Configuring the Initial Context Factory".
To look up the remote interface of an enterprise bean using an ejb-ref
, do the following:
Define an ejb-ref
element for the enterprise bean in the ejb-jar.xml
file.
Example 29-4 ejb-jar.xml For an ejb-ref Element
<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: Clustered or Combined Web Tier and EJB Tier").
Determine whether or not a prefix is required (see "Selecting an EJB Reference").
Look up the enterprise bean using the ejb-ref-name
element and the appropriate prefix (if required).
Example 29-5 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".
To look up the remote interface of an EJB using its location
, do the following:
Define the location
attribute for a entity-deployment
element in the orion-ejb-jar.xml
file.
Example 29-6 orion-ejb-jar.xml for location Attribute
<entity-deployment name="Test" location="app/Test" ... > ... </entity-deployment>
The default value for location
attribute is the value of entity-deployment
attribute name
.
Determine whether or not a prefix is required (see "Selecting an EJB Reference").
Look up the enterprise bean using the location
.
Example 29-7 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".
To look up the remote interface of an EJB using an ejb-local-ref
, do the following:
Define an ejb-local-ref
element for the enterprise bean in the ejb-jar.xml
file.
Example 29-8 ejb-jar.xml For an ejb-local-ref Element
<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").
Determine whether or not a prefix is required (see "Selecting an EJB Reference").
Look up the enterprise bean using the ejb-ref-name
and the appropriate prefix (if required).
Example 29-9 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".
To look up the local interface of an EJB using its local-location
, do the following:
Define the local-location
attribute of the entity-deployment
element in the orion-ejb-jar.xml
file.
Example 29-10 orion-ejb-jar.xml for local-location Attribute
<entity-deployment name="Test" local-location="app/Test" ... > ... </entity-deployment>
The default value for local-location
is the value of entity-deployment
attribute name
.
Determine whether or not a prefix is required (see "Selecting an EJB Reference").
Look up the enterprise bean using the local-location
.
Example 29-11 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".