Search code examples
javaejbcdi

Injecting EJB via interface in Wildfly application


I am using wildfly 10.1.0 and JavaEE 7

I've got this interface:

public interface TestEjb {
    String something();
}

and this Ejb class implementing it:

@LocalBean
@Stateless
public class TestEjbImpl implements TestEjb {

    @Override
    public String something() {
        return "Hello world";
    }
}

When I am injecting it to my @Path annotated jax-rs class using

@Inject
private TestEjb testEjb;

It gives an error saying "WELD-001408: Unsatisfied dependencies for type TestEjb with qualifiers @Default"

But when I inject it like

@Inject
private TestEjbImpl testEjb;

it works fine. And which is surprising both ways work with no problems in jboss-eap-6.4. But why?


Solution

  • First of all, you are mixing CDI injection with EJB injection. Rather use @EJB (instead of @Inject) when injecting your EJB.

    @LocalBean has a no-interface view. So, you have an interface with no view annotation and a bean with a no-interface view annotation. The EJB Container understands it as a no interface view.

    The best way will be to annotate the TestEjb interface with @Local view and remove the @LocalBean from the TestEjbImpl implementation in order for your solution to work.

    Interface

    @Local
    public interface TestEjb {
        String something();
    }
    

    EJB

    @Stateless
    public class TestEjbImpl implements TestEjb {
    
        @Override
        public String something() {
            return "Hello world";
        }
    }
    

    Injection time

    @EJB
    private TestEjb testEjb;
    

    I hope this helps.

    Further reading...