The Java EE 6 Tutorial, Volume I

Local Clients

A local client has these characteristics:

The no-interface view of an enterprise bean is a local view. The public methods of the enterprise bean implementation class are exposed to local clients that access the no-interface view of the enterprise bean. Enterprise beans that use the no-interface view do not implement a business interface.

The local business interface defines the bean’s business and lifecycle methods. If the bean’s business interface is not decorated with @Local or @Remote, and the bean class does not specify the interface using @Local or @Remote, the business interface is by default a local interface.

To build an enterprise bean that allows only local access, you may, but are not required to do one of the following:

Accessing Local Enterprise Beans Using the No-Interface View

Client access to an enterprise bean that exposes a local, no-interface view is accomplished either through dependency injection or JNDI lookup.

Clients do not use the new operator to obtain a new instance of an enterprise bean that uses a no-interface view.


Example 14–2 Injecting an Enterprise Bean Using the No-Interface View

To obtain a reference to the no-interface view of an enterprise bean through dependency injection, use the javax.ejb.EJB annotation and specify the enterprise bean's implementation class.

@EJB
ExampleBean exampleBean;


Example 14–3 Looking Up an Enterprise Bean Using the No-Interface View

To obtain a reference to the no-interface view of an enterprise bean using JNDI lookup, use the javax.naming.InitialContext interface's lookup method.

ExampleBean exampleBean = (ExampleBean) 
        InitialContext.lookup("java:module/ExampleBean");

Accessing Local Enterprise Beans That Implement Business Interfaces

Client access to enterprise beans that implement local business interfaces is accomplished using either dependency injection or JNDI lookup.


Example 14–4 Injecting an Enterprise Bean's Local Business Interface

To obtain a reference to the local business interface of an enterprise bean through dependency injection, use the javax.ejb.EJB annotation and specify the enterprise bean's local business interface name.

@EJB
Example example;


Example 14–5 Looking Up a Local Enterprise Bean Using JNDI

The obtain a reference to a local business interface of an enterprise bean using JNDI lookup, use the javax.naming.InitialContext interface's lookup method.

ExampleLocal example = (ExampleLocal)
         InitialContext.lookup("java:module/ExampleLocal");