Ariod
Ariod

Reputation: 5851

Java EE, injecting one EJB into another EJB

I've encountered one issue I can't seem to figure out. I would like to inject one EJB into another like this:

@Stateless
public class MainEJB {

    @EJB
    private HelperEJB helper;

}

@Stateless
public class HelperEJB implements HelperInterface {

}

As you can see, HelperEJB is exposed through a no-interface view (note: HelperInterface is an interface from an external library, it is not an EJB interface). This does not work, and I will get the following exception:

 javax.naming.NamingException: Lookup failed for 'org.mycompany.ejb.HelperEJB #org.mycompany.ejb.HelperEJB'

However, if HelperEJB does not implement any interface:

@Stateless
public class HelperEJB {

}

It will work. It will also work if I have a @Local interface standing between MainEJB and HelperEJB.

Why this cannot be done through a no-interface view like in my first attempt?

Upvotes: 3

Views: 1934

Answers (2)

Arjan Tijms
Arjan Tijms

Reputation: 38163

A no-interface view is only created when the EJB does not implement any (business) interface.

You can explicitly declare that you want a no-interface view by using the @LocalBean annotation.

Upvotes: 2

deltaforce2
deltaforce2

Reputation: 593

Your first example should work if your private field is declared using the interface, like this:

@Stateless
public class MainEJB {
    @EJB
    private HelperInterface helper; // changed this
}

@Stateless
public class HelperEJB implements HelperInterface {
}

@Local
public interface HelperInterface {
}

Upvotes: 3

Related Questions