Java – How to select the local interface for an EJB

ejbejb-3.0jakarta-eejava

Say I have the following EJB (using ejb3):

@Stateless(name="Queries")
@Remote(Queries.class)
@Local(Queries.class)
public final class QueriesEJB implements Queries {
    ...
}

The class is available through both a local and a remote interface.

How can I inject the local interface for this EJB in another part of the app?

Specifically, I'm not sure how to create an @EJB annotation that selects the local interface. For example, is the following sufficient?

@EJB(name="Queries") private Queries queries;

In particular I want to avoid creating separate local and remote interfaces simply for the purpose of distinguishing via @EJB's 'beanInterface' property.

Best Answer

According to the spec you cannot have an interface that is Remote and Local at the same time. However, you create a super-interface, put all methods there, and then create 2 sub-interfaces. Having done that, simply use @EJB. This way you need to maintain only one interface at all.

EDIT: See section 3.2 in "EJB3 spec simplified" at http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html