8.1.12

EJB 3.x: One Interface Two Implementations

Note: This entry is about handling the issue "One Interface Two Implementations" for Enterprise Java Beans (EJB) 3.x. Fixing such a problem for the Context Dependency Injection (CDI) framework is done differently and will be covered by another entry.

Problem
If you provide two implementations within your WAR or EAR file for just one interface and inject it using this code

@EJB
private MyInterface myInterface;

the application server will complain as soon as you try to deploy your archive; e.g. Glassfish throws an exception looking something like that:

Cannot resolve reference Remote ejb-ref name=at.rt.ClientBean/myInterface,Remote 3.x interface =at.rt.sameinterface.MyInterface,ejb-link=null,lookup=,mappedName=, jndi-name=,refType=Session because there are 2 ejbs in the application with interface at.rt.sameinterface.MyInterface.

 
Solution
If you want to keep your annotation, simply use the following code to resolve the lookup problem:

@EJB(beanName="MyInterfaceOneBean")
private MyIntterface myInterface;

The other possibility is to modify your deployment descriptor and add an entry similar to that:

<ejb-jar ...>
 <enterprise-beans>
    <session>
        <ejb-name> ClientBean </ejb-name>
        <ejb-local-ref>
            <ejb-ref-name>at.rt.ClientBean/myInterface</ejb-ref-name>
            <local>at.rt.sameinterface.MyInterface </local>
            <ejb-link>MyInterfaceOneBean</ejb-link>
        </ejb-local-ref>
    </session>
</enterprise-beans>
</ejb-jar>

No comments:

Post a Comment